Data types
Numbers
pythonOut · result2
pythonOut · result3
pythonOut · result0.5
pythonOut · result16
pythonOut · result0
pythonOut · result1
pythonOut · result50
Variable Assignment
python# Can not start with number or special characters
name_of_var = 2
pythonpythonpythonOut · result5
Strings
pythonOut · result'single quotes'
pythonOut · result'double quotes'
python" wrap lot's of other quotes"
Out · result" wrap lot's of other quotes"
Printing
pythonpythonOut · result'hello'
pythonOut · stdouthello
pythonpythonprint('My number is: {one}, and my name is: {two}'.format(one=num,two=name))
Out · stdoutMy number is: 12, and my name is: Sam
pythonprint('My number is: {}, and my name is: {}'.format(num,name))
Out · stdoutMy number is: 12, and my name is: Sam
Lists
pythonOut · result[1, 2, 3]
pythonOut · result['hi', 1, [1, 2]]
pythonpythonpythonOut · result['a', 'b', 'c', 'd']
pythonOut · result'a'
pythonOut · result'b'
pythonOut · result['b', 'c', 'd']
pythonOut · result['a']
pythonpythonOut · result['NEW', 'b', 'c', 'd']
pythonnest = [1,2,3,[4,5,['target']]]
pythonOut · result[4, 5, ['target']]
pythonOut · result['target']
pythonOut · result'target'
Dictionaries
pythond = {'key1':'item1','key2':'item2'}
pythonOut · result{'key1': 'item1', 'key2': 'item2'}
pythonOut · result'item1'
Booleans
pythonOut · resultTrue
pythonOut · resultFalse
Tuples
pythonpythonOut · result1
pythonOut · errorTypeError: 'tuple' object does not support item assignment
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-44-97e4e33b36c2> in <module>()
----> 1 t[0] = 'NEW'
2 more lines
TypeError: 'tuple' object does not support item assignment
Sets
pythonOut · result{1, 2, 3}
python{1,2,3,1,2,1,2,3,3,3,3,2,2,2,1,1,2}
Out · result{1, 2, 3}
Comparison Operators
pythonOut · resultFalse
pythonOut · resultTrue
pythonOut · resultTrue
pythonOut · resultTrue
pythonOut · resultTrue
pythonOut · resultFalse
Logic Operators
pythonOut · resultFalse
pythonOut · resultTrue
python(1 == 2) or (2 == 3) or (4 == 4)
Out · resultTrue
if,elif, else Statements
pythonOut · stdoutYep!
pythonOut · stdoutyep!
pythonif 1 < 2:
print('first')
else:
print('last')
Out · stdoutfirst
pythonif 1 > 2:
print('first')
else:
print('last')
Out · stdoutlast
pythonif 1 == 2:
print('first')
elif 3 == 3:
print('middle')
else:
print('Last')
Out · stdoutmiddle
for Loops
pythonpythonfor item in seq:
print(item)
Out · stdout1
2
3
4
5
pythonfor item in seq:
print('Yep')
Out · stdoutYep
Yep
Yep
Yep
Yep
pythonfor jelly in seq:
print(jelly+jelly)
Out · stdout2
4
6
8
10
while Loops
pythoni = 1
while i < 5:
print('i is: {}'.format(i))
i = i+1
Out · stdouti is: 1
i is: 2
i is: 3
i is: 4
range()
pythonOut · resultrange(0, 5)
pythonfor i in range(5):
print(i)
Out · stdout0
1
2
3
4
pythonOut · result[0, 1, 2, 3, 4]
list comprehension
pythonpythonout = []
for item in x:
out.append(item**2)
print(out)
Out · stdout[1, 4, 9, 16]
pythonOut · result[1, 4, 9, 16]
functions
pythondef my_func(param1='default'):
"""
Docstring goes here.
"""
print(param1)
pythonOut · result<function __main__.my_func>
pythonOut · stdoutdefault
pythonOut · stdoutnew param
pythonmy_func(param1='new param')
Out · stdoutnew param
pythondef square(x):
return x**2
pythonpythonOut · stdout4
lambda expressions
pythondef times2(var):
return var*2
pythonOut · result4
pythonOut · result<function __main__.<lambda>>
map and filter
pythonpythonOut · result<map at 0x105316748>
pythonOut · result[2, 4, 6, 8, 10]
pythonlist(map(lambda var: var*2,seq))
Out · result[2, 4, 6, 8, 10]
pythonfilter(lambda item: item%2 == 0,seq)
Out · result<filter at 0x105316ac8>
pythonlist(filter(lambda item: item%2 == 0,seq))
Out · result[2, 4]
methods
pythonst = 'hello my name is Sam'
pythonOut · result'hello my name is sam'
pythonOut · result'HELLO MY NAME IS SAM'
pythonOut · result['hello', 'my', 'name', 'is', 'Sam']
pythontweet = 'Go Sports! #Sports'
pythonOut · result['Go Sports! ', 'Sports']
pythonOut · result'Sports'
pythonOut · result{'key1': 'item1', 'key2': 'item2'}
pythonOut · resultdict_keys(['key2', 'key1'])
pythonOut · resultdict_items([('key2', 'item2'), ('key1', 'item1')])
pythonpythonOut · result3
pythonOut · result[1, 2]
pythonOut · resultFalse
pythonOut · resultTrue
Great Job!