Main Reference: McKinney, Wes. Python for Data Analysis: Data Wrangling with Pandas, NumPy, and IPython. O'Reilly Media. Kindle Edition
Python code is structured by indentation (tabs or spaces) instead of braces which is what other languages normally use. In addition, a colon (:) is used to define the start of an indented code block.
In [ ]:
for x in list(range(5)):
print("One number per loop..")
print(x)
if x > 2:
print("The number is greater than 2")
print("----------------------------")
In [ ]:
a = "pedro"
In [ ]:
a.capitalize()
In Python, when you define/create a variable, you are basically creating a reference to an object (i.e string,list,etc). If you want to define/create a new variable from the original variable, you will be creating another reference to the original object rather than copying the contents of the first variable to the second one.
In [ ]:
a = [1,2,3]
b = a
b
Therefore, if you update the original variable (a), the new variable (b) will automatically reference the updated object.
In [ ]:
a.append(4)
b
A variable can have a short name (like x and y) or a more descriptive name (age, dog, owner). Rules for Python variables:
Reference:https://www.w3schools.com/python/python_variables.asp
In [ ]:
dog_name = 'Pedro'
age = 3
is_vaccinated = True
birth_year = 2015
In [ ]:
is_vaccinated
In [ ]:
dog_name
As any other object, you can get information about its type via the built-in function type().
In [ ]:
type(age)
In [ ]:
type(dog_name)
In [ ]:
type(is_vaccinated)
In [ ]:
x = 4
y = 10
In [ ]:
x-y
In [ ]:
x*y
In [ ]:
y/x
In [ ]:
y**x
In [ ]:
x>y
In [ ]:
x==y
In [ ]:
y>=x
In [ ]:
2+4
In [ ]:
5*6
In [ ]:
5>3
In [ ]:
print("Hello Helk!")
In [ ]:
print("x = " + str(x))
print("y = " + str(y))
In [ ]:
if x==y:
print('yes')
else:
print('no')
In [ ]:
if x==y:
print('They are equal')
elif x > y:
print("It is grater than")
else:
print("None of the conditionals were true")
In [ ]:
my_dog_list=['Pedro',3,True,2015]
In [ ]:
for i in range(0,10):
print(i*10)
In [ ]:
i = 1
while i <= 5:
print(i ** 2)
i += 1
In [ ]:
i = 1
while i > 0:
if i > 5:
break
print(i ** 2)
i += 1
In [ ]:
my_dog_list=['Pedro',3,True,2015]
In [ ]:
my_dog_list[0]
In [ ]:
my_dog_list[2:4]
In [ ]:
print("My dog's name is " + str(my_dog_list[0]) + " and he is " + str(my_dog_list[1]) + " years old.")
list.append()
which allows you to add an item to the end of the list. Equivalent to a[len(a):] = [x]
.
In [ ]:
my_dog_list.append("tennis balls")
In [ ]:
my_dog_list
In [ ]:
my_dog_list[1] = 4
my_dog_list
In [ ]:
my_dog_dict={'name':'Pedro','age':3,'is_vaccinated':True,'birth_year':2015}
In [ ]:
my_dog_dict
In [ ]:
my_dog_dict['age']
In [ ]:
my_dog_dict.keys()
In [ ]:
my_dog_dict.values()
In [ ]:
my_dog_tuple=('Pedro',3,True,2015)
In [ ]:
my_dog_tuple
In [ ]:
my_dog_tuple[1]
In [ ]:
seq = [ 7 , 2 , 3 , 7 , 5 , 6 , 0 , 1 ]
seq [ 1 : 5 ]
In [ ]:
def square(n):
return n ** 2
In [ ]:
print("Square root of 2 is " + str(square(2)))
In [ ]:
number_list = [1,2,3,4,5]
In [ ]:
for number in number_list:
sn = square(number)
print("Square root of " + str(number) + " is " + str(sn))
In [ ]:
def square(n):
return n ** 2
def cube(n):
return n ** 3
In [ ]:
import math_ops
In [ ]:
for number in number_list:
sn = square(number)
cn = cube(number)
print("Square root of " + str(number) + " is " + str(sn))
print("Cube root of " + str(number) + " is " + str(cn))
print("-------------------------")
In [ ]:
help('modules')
In [ ]:
import datetime
In [ ]:
dir(datetime)
In [2]:
import datetime as dt
In [3]:
dir(dt)
Out[3]: