Python Variables
Python Variables
Week1
print(some_answer)
>>>42
some_
answe
print(type(some_answer)) r
>>>int
3
Naming variables
Can you guess what is in each box?
cat c var_
1
Variables: Naming convention
'some_variable'
'NOT_GLOBAL_VAR' ----
'Averagecamelsizeindesert' tempCelsius
'number-of-cans-in-a-sixpack' stationID
'xxrstaqwe' currentTimestamp
5
Basic operations with variables
6
Comments
# This is a comment before some code '''
print("Hello Python!") multi-row
comment
'''
7
Data Type: String my_string = "Burj Khalifa"
string_three += "!"
>>>Expo2020 tookplace in 2021 in Dubai!
10
String methods
dir(my_string)
>>>[‘__add__’, ‘__class__’, ‘__contains__’, ‘__delattr__’, ‘__doc__’, ‘__eq__’, ‘__format__’,
‘__ge__’, ‘__getattribute__’, ‘__getitem__’, ‘__getnewargs__’, ‘__getslice__’, ‘__gt__’,
‘__hash__’, ‘__init__’, ‘__le__’, ‘__len__’, ‘__lt__’, ‘__mod__’, ‘__mul__’, ‘__ne__’, ‘__new__’,
‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__rmod__’, ‘__rmul__’, ‘__setattr__’,
‘__sizeof__’, ‘__str__’, ‘__subclasshook__’, ‘_formatter_field_name_split’,
‘_formatter_parser’, ‘capitalize’, ‘center’, ‘count’, ‘decode’, ‘encode’, ‘endswith’,
‘expandtabs’, ‘find’, ‘format’, ‘index’, ‘isalnum’, ‘isalpha’, ‘isdigit’, ‘islower’, ‘isspace’,
‘istitle’, ‘isupper’, ‘join’, ‘ljust’, ‘lower’, ‘lstrip’, ‘partition’, ‘replace’, ‘rfind’, ‘rindex’,
‘rjust’, ‘rpartition’, ‘rsplit’, ‘rstrip’, ‘split’, ‘splitlines’, ‘startswith’, ‘strip’, ‘swapcase’,
‘title’, ‘translate’, ‘upper’, ‘zfill’]
11
Data Type: Integers and Floats
13
Data Type: Boolean
Boolean values represent True/False statements.
Often used to check for None, empty strings, list, objects, etc.
my_bool = 2 + 2 == 5
my_bool, type(my_bool)
>>>False, <class 'bool'>
14
Data Type: List
my_string = [4,8,15,16,23,42]
my_list[xstart:xend:step] my_list[0] = 1
my_list[0] >>>4 >>>[1, 8, 15, 16, 23, 42]
my_list[-1] >>>42
my_list[1:4] >>>[8, 15, 16] my_list = my_list + [100]
my_list[3:] >>>[16, 23, 42] >>>[1, 8, 15, 16, 23, 42, 100]
my_list[::-1] >>>[42, 23, 16, 15, 8, 4]
my_list[:] >>>[4, 8, 15, 16, 23, 42] del my_list[-2]
>>>[1, 8, 15, 16, 23, 100]
15
List methods
dir(my_list)
>>>['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__',
'__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__',
'__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__',
'__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__',
'__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count',
'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
16
Data Type: Tuple
my_tuple = (16,2,4,8)
my_tuple[-1] >>>8
my_tuple[1:3] >>>(2, 4)
17
Data Type: Set
my_set = {1, 2, 4, 8, 16, 32}
del my_dictionary['completed']
>>>{
"name" : "Burj Khalifa",
"height_m" : 829.8,
"hashtags" : ["#burjkhalifa",
"#dubai", "#uae", "#dubaimall"]
}
20
Dictionary methods
dir(my_dictionary)
>>>['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__',
'__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__',
'__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__',
'__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear',
'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update',
'values']
21
Data Type comparison