-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotes_PythonBasics.txt
More file actions
104 lines (80 loc) · 3.13 KB
/
Notes_PythonBasics.txt
File metadata and controls
104 lines (80 loc) · 3.13 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
95
96
97
98
99
100
101
102
103
104
Order of Operation --
=> PEMDAS - Parentheses, Exponents, Multiplication/Division, Addition/Substraction
Basic Arithmetic operators --
=> + , - , * , / , //, % , **
help() -- brief about built-in functions
=> e.g. help(round), help(print),...
Note: (Docstring) you can also use help() for user-defined functions brief by providing a """ brief """ inside the user-defined function.
e.g.
#Code
def least_difference(a, b, c):
"""Return the smallest difference between any two numbers
among a, b and c.
>>> least_difference(1, 5, -5)
4
"""
diff1 = abs(a - b)
diff2 = abs(b - c)
diff3 = abs(a - c)
return min(diff1, diff2, diff3)
#Input
help(least_difference)
#Output
Help on function least_difference in module __main__:
least_difference(a, b, c)
Return the smallest difference between any two numbers
among a, b and c.
>>> least_difference(1, 5, -5)
4
Higher-Order Functions --
=> Functions that operate on onther functions
*************************************************************************
Order of Precedence for Boolean Operators --
=> not >> and >> or
List -- []
=> ordered sequences of values.
=> able to contain different types of variables.
=> Indexing is useful to access individual list elements. (starts from 0)
Note: negative values refers from end of list
=> Slicing - list_name['start point':'end point']
Note: this will return all list elements from start point and continuing to but not end point.
=> lists are mutable (can be modified)
=> List Functions:-
len() , sorted() , sum() , max() , min()
=> List Methods:-
append() , pop() , index()
Tuples -- ()
=> almost similar to list.
=> Tuples are immutable (cannot be modified).
*************************************************************************
Loops --
=> for (variable) in (object that supports iteration):
...
=> range() -- for (variable) in range():
...
=> while (conditio):
...
List comprehensions --
=> sqr = [n**2 for n in range(10)]
[0,1,4,9,16,25,36,49,64,81]
--Monte Carlo Method
*************************************************************************
Strings --
=> remember ' ' can have " " and vice-versa " " can have ' '.
Note:- \ allows to have single quote (') inside the single quoted string ' '.
=> Single quote (' '), Double quote (" "), Triple quote (""" """)
=> Strings -- sequence of characters.
=> Strings are immutable (cannot be modified).
String Methods --
=> upper() , lower() , index() , startswith() , endswith() , split() , join() , format()
*************************************************************************
Dictionaries {} --
=> built-in Python Data Structure - map keys to values
=> access keys using []
=> keys() , values() , items() -- Methods
*************************************************************************
These 3 tools will guide one to understand strange objects --
=> type() - what is this thing?
=> dir() - what can I do with it?
=> help() - tell me more!
*************************************************************************