-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimportant_python_concepts.py
More file actions
59 lines (41 loc) · 2.12 KB
/
important_python_concepts.py
File metadata and controls
59 lines (41 loc) · 2.12 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
#! 1. Mutable v/s Immutable types
# ! Immutable or cannot change types are:
str
int
float
bool
bytes
tuple
#! Mutable types are
list
set
dict
# example
x = (1, 2)
y = x
# cannot do x[0] = 3
x = (1, 2, 3)
# y is still (1, 2)
def func(x, y, z):
print(x, y, z)
func(1, 2, 3)
func(x=1, z=2, y=3)
func(5, z=2, y=1)
# start with the positional arguements then do the named or keyword args
def f(x, y, z=2): # example of a functino with optional args
print(x, y, z)
def f2(x,y, *args):
print(x, y, args)
# ! *args allows us to accept any number of positional arguements inside a tuple called args in the function
f2(2, 3,4 ,5 ,6 ,6, 4)
# ! **kwargs allows us to accept any number of keyword arguements
def f3(*args, **kwargs):
print(args, kwargs)
f3(2, 3, 5, 2, 1, 5, name='tom', age=32, color='r', is_gay='false')
#! how to use dynamic unpacking while invoking the function
def f4(a, b, c=True, d= False):
print(a, b, c, d)
f4(*[3, 1], **{'c':'hello', 'd': 5.932421})
#! meaning of if __name__ == "__main__" : simply tells you whether or not you're running the current python file, it puts the code that you want to run whne executing this file inside the if so tht when this file's contents are imported the code inside the if doesn't run
#! GIL - Global Interpreter Lock, this is exclusive to python and essentially what this says is that any threads that needs to be executed needs to acquire the interpreter lock what that means is that you can only execute one thread at any point of time even if you have multiple cpu cores on your computer,
#! on your computer you have a cpu, that cpu typically has multiple cores 2,4, 8, or anything, each core can execute one operation at a time, with hyperthreading and virtualization you may be able to do a few more but thats not the scope here, in other languages you can break your code into seperate threads and run those threads indepently on different cores and get performance benefits, but in python you cannot do that, even if you break it into threads and run them on different cores due the GIL only one thread can be executed at a time and there is no change in performance.