This notebook will just go through the basic topics in order:
1 + 1
2
1 * 3
3
1 / 2
0.5
2 ** 4
16
4 % 2
0
5 % 2
1
(2 + 3) * (5 + 5)
50
# Can not start with number or special characters
name_of_var = 2
x1=2
#float
x = 2
y = 3
z = x + y
z
5
'single quotes'
'single quotes'
"double quotes"
'double quotes'
" wrap lot's of other quotes"
" wrap lot's of other quotes"
x = 'hello'
x
'hello'
print(x)
hello
num = 12
name = 'Sam'
print('My number is: {one}, and my name is: {two}'.format(one=12,two='Winfred'))
My number is: 12, and my name is: Claire
print('My number is: {}, and my name is: {}'.format(num,name))
My number is: 12, and my name is: Sam
[1,2,3]
[1, 2, 3]
['hi',1,[1,2]]
['hi', 1, [1, 2]]
my_list = ['a','b','c']
my_list.append('d')
my_list
['a', 'b', 'c', 'd']
#Indexing- accessing the elements
my_list[0]
'a'
my_list[1]
'b'
#Slicing(aided by a full colon)
my_list[0:]#all elements in the list
my_list[1:]#returns from the second element to the end of the list
my_list[:2]#returns the first two elements
my_list[1:3] #returns the elements in the second and third
['b', 'c']
my_list[:1]
['a']
#Replacing elements in a list
my_list[0] = 'NEW'
my_list[3] = 'OLD'
my_list
['NEW', 'b', 'c', 'OLD']
#Nested Lists
nest = [1,2,3,[4,5,['target']]]
nest[3]
nest1 = [1,2,3,[4,5,['target']],[4,8]]
nest1[4]
[4, 8]
#Returns the list as per the indices
nest[3][2]
['target']
#Returns the actual value
nest[3][2][0]
'target'
d = {'key1':'item1','key2':'item2'}
students_marks={'Claire':50,'Carol':60, 'Caleb':34}
students_marks['Caleb']
34
d
{'key1': 'item1', 'key2': 'item2'}
d['key1']
'item1'
True
True
False
False
t = (1,2,3)
t[0]
1
t[0] = 'NEW'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-45-93bfe9be1549> in <module>()
----> 1 t[0] = 'NEW'
TypeError: 'tuple' object does not support item assignment
{1,2,3}
{1, 2, 3}
{1,2,3,1,2,1,2,3,3,3,3,2,2,2,1,1,2}
{1, 2, 3}
1 > 2
False
1 < 2
True
1 >= 1
True
1 <= 4
True
1 == 1
True
'hi' == 'bye'
False
(1 > 2) and (2 < 3)
False
(1 > 2) or (2 < 3)
True
(1 == 2) or (2 == 3) or (4 == 4)
True
if 1 < 2:
print('Ohhh Yaa!')
Ohhh Yaa!
def checkNumber(x):
if x==0:
print("The number you entered is 0,neither odd nor even.")
elif x%2==0:
print("The number",x,"is even")
else:
print("The number",x,"is odd")
while True:
x = int(input("Enter a number: "))
checkNumber(x)
Enter a number: 4
The number 4 is even
if 1 < 2:
print('Ahh True!')
Ahh True!
if 1 < 2:
print('first')
else:
print('last')
first
if 1 > 2:
print('first')
else:
print('last')
last
if 1 == 2:
print('first')
elif 3 == 3:
print('middle')
else:
print('Last')
middle
seq = [1,2,3,4,5]
for item in seq:
print(item)
1
2
3
4
5
for item in seq:
print('Present')
Present
Present
Present
Present
Present
for jelly in seq:
print(jelly+jelly)
2
4
6
8
10
i = 1
while i < 5:
print('i is: {}'.format(i))
i = i+1
i is: 1
i is: 2
i is: 3
i is: 4
range(5)
range(0, 5)
for i in range(5):
print(i)
0
1
2
3
4
list(range(5))
[0, 1, 2, 3, 4]
x = [1,2,3,4]
out = []
for item in x:
out.append(item**2)
print(out)
[1, 4, 9, 16]
def myName(name):
print(name)
name = "Steven"
myName(name)
Steven
[item**2 for item in x]
[1, 4, 9, 16]
def my_func(param1='default'):
"""
Docstring goes here.
"""
print(param1)
my_func
<function __main__.my_func>
my_func()
default
my_func('new param')
new param
my_func(param1='new param')
new param
def square(x):
return x**2
out = square(2)
print(out)
4
def times2(var):
return var*2
times2(2)
4
lambda var: var*2
<function __main__.<lambda>>
seq = [1,2,3,4,5]
map(times2,seq)
<map at 0x1e2821432e8>
list(map(times2,seq))
[2, 4, 6, 8, 10]
list(map(lambda var: var*2,seq))
[2, 4, 6, 8, 10]
filter(lambda item: item%2 == 0,seq)
<filter at 0x1e282101668>
list(filter(lambda item: item%2 == 0,seq))
[2, 4]
st = 'hello my name is Sam'
st.lower()
'hello my name is sam'
st.upper()
'HELLO MY NAME IS SAM'
st.split()
['hello', 'my', 'name', 'is', 'Sam']
tweet = 'Go Sports! #Sports'
tweet.split('#')
['Go Sports! ', 'Sports']
tweet.split('#')[1]
'Sports'
d
{'key1': 'item1', 'key2': 'item2'}
d.keys()
dict_keys(['key1', 'key2'])
d.items()
dict_items([('key1', 'item1'), ('key2', 'item2')])
lst = [1,2,3]
lst.pop()
3
lst
[1, 2]
'x' in [1,2,3]
False
'x' in ['x','y','z']
True