-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_fundamentals.txt
More file actions
94 lines (63 loc) · 2.8 KB
/
python_fundamentals.txt
File metadata and controls
94 lines (63 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#Iterate through reversed list and
for index, element in reversed(enumerate(d)):
#iterate through a reversed list and print the index, elemnet after reversed
for index, element in reversed(list(enumerate(d))):
#iterate through a reversed list and print the original index, element before reversing
#when iterating through some loop and accessing indexes of array elemnets
#have the iterator variable = the current iteration, it becomes simpler to think about
#dont splice list when you need to maintain its index
#iterate through a dictionary doing the following
#python3
for key, value in d.items():
print(key)
print(value)
you cant do this
element = "4 5 6"
[int(d), b , c] = element.split()
you have to cast d after
#ALso remember that when you are doing division in python3 7/2 will equal 3.5 and not 3, which is not the case in python2
if you would like to join several elements in a list together and form a string with dashes in the middle
d=['a','b','c']
'-'.join(d)
#returns all the elements in the array after and including the first element
d[1:]
#returns index 0 and index 1 of d
d[0:2]
#returns index 0 and 1 of d
d[:2]
#iterates through elemnets 0, 6 by values of 2
range(0, 6, 2)
Scope:
variables declared inside an if statement, while and for loops are still assigned to the functiono, class or module unlike java. A variable not existing yet is different from it being None. for example. A name error is thrown if a variable doesnt exist and tries to be used
#In this case a is not defined at all and a Name Error is thrown because it doesnt exist. One can always use a try and except block to atch the NameError but rather it is cleaner to define a to be None prior.
if (not a):
d = c*y
a = None
if not a:
d = c*y
Some python objects are immutable which means that they can not be changed but rather return new ojects when attempting to update. For example int float deecimal copmlex bool string tuple range frozen set bytes are all immutable. When its value is changed it returns a new object.
Lets say we do the following
a = b = [7,8,9]
and then we do a.pop()
id(a) == id(b) will be True afterwards. The reason for this is because a and b are immutable objectsi
The values that are mutable are list, dict, set, bytearray (i.e elements that have methods that can change the value). (Tuples are not mutable)
Call by reference
def updateList(list1)
list1 += 10
def updateList(n):
n += [10]
n = [5,6]
print(id(n))
print(n)
updateList(n)
print(n)
print(id(n))
we have called the list via call by reference so the changes are made to the original list themselves
In python2 if you want to print without a new line at the end you can simply put a comma like this
print root.data,
Bitwise operation:
x & y
does a bitwise and
x|y bit wise or
~ x bit wise not
x ^ y bit wise exclusive or