name_of_var = 2 #can't have spaces or start with a number or special character
name_of_var #call for the variable
print(name_of_var) #print values
num = 50
type(num) #integer
num1 = 4.75
type(num1) #float
cn = complex(3,4)
type(cn) #complex number
st = 'hola!'
type(st) #string
boo = True
type(boo) #boolean
lst = [1,2,3]
type(lst) #list
d = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
type(d) #dictionary
t = (1,2,3)
type(t) #tuple
s = {1,2,3}
type(s) #set
fnc = lambda var: var*2
type(fnc) #function
str(num) #number to string
int(num1) #float to the nearest integer
float(num) #integer to float
bool(0) #False
bool(1) #True
list(t) #tuple to list
tuple(lst) #list to tuple
list(d) #dictionaty to list (only keys)
1 + 1 #sum
5 - 2 #sustraction
3 * 4 #product
8 / 2 #division
9 % 2 #module
2 ** 3 #power
x = 3
y = 6
res = (x * y) - (x + y)
res
(2 + 3) * (5 + 5) #combinated operations
st1 = 'single quotes'
st1
st2 = "double quotes"
st2
st1 + ' or ' + st2 #concatenate strings
#assign variables
name = 'Paul'
num = 28
#include variables in text
print('My name is {one}, and my age is {two}'.format(one=name,two=num))
print('My name is {}, and my age is {}'.format(name,num))
[1,2,3] #same data type
['hi',1,[1,2]] #different type
my_list = ['a','b','c']
my_list.append('d') #add value
my_list
d = {'key1':'value1','key2':'value2','key3':'value3'}
d
True & True #& means AND
True & False
False & False
True | True #& means OR
True | False
False | False
t = (1,2,3)
t[0]
#t[0] = 4 #ERROR: tuples can't be modified
{1,2,3}
{1,2,3,1,2,1,2,3,3,3,3,2,2,2,1,1,2} #unique values
my_list[0] #one value using position (Iindex starts at 0)
my_list[1:3] #many values using range (start is included, end is excluded.)
my_list[:1] #if no value means from the beggining/ until the end
my_list[0] = 'X' #modify value
my_list
nest = [1,2,3,[4,5,[7,8]]] #nested list
nest[3][2][0] #selects the corresponded number of each nested item
d['key1'] #selection in dictionaries using key
1 < 2 #smaller than
1 > 2 #bigger than
1 >= 1 #bigger or equal
1 <= 4 #smaller or equal
'hola' == 'hi' #equal
'hola' != 'hi' #different
(1 > 2) and (2 < 3) #AND means ALL
(1 > 2) & (2 < 3) #equivalent
(1 > 2) or (2 < 3) #OR means ANY
(1 > 2) | (2 < 3) #equivalent
'x' in [1,2,3] #included
'x' in ['x','y','z']
if 1 < 2:
print('yes') #if true print
if 1 < 2:
print('first')
else:
print('last') #if true print, else print
if 1 == 2:
print('first')
elif 3 == 3:
print('middle')
else:
print('Last') #if true print, elif true print, else print
seq = [1,2,3,4,5]
for item in seq:
print(item)
for item in seq:
print('number')
#while
i = 1
while i < 5:
print('i is: {}'.format(i))
i = i+1
#range
range(5)
for i in range(5):
print(i)
#list comprehension
x = [1,2,3,4]
out = []
for item in x:
out.append(item**2)
print(out)
[item**2 for item in x] #equivalent in one line
def my_func(param1='default'):
"""
Description.
"""
print(param1)
my_func()
def square(x):
return x**2
square(2)
def times2(var):
return var*2
times2(2)
lambda var: var*2 #equivalent
#map
seq = [1,2,3,4,5]
list(map(times2,seq))
list(map(lambda var: var*2,seq)) #equivalent
#filter
list(filter(lambda item: item%2 == 0,seq))
num = 4.24
round(num)
st = 'Winter is coming!'
st.lower() #lower case
st.upper() #upper case
st.capitalize() #capitalize first word
stsp = st.split() #split using spaces
stsp
tweet = 'Winter is coming! #GOT'
tweet.split('#')[1] #split using special character and selecting after it
d.keys() #for dictionaries, keys
d.items() #for dictionaries, items
lst = [4,5,1,2,3]
lst
min(lst)
max(lst)
sorted(lst)
lst.pop() #drops last element
lst