-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntax cheatsheet.txt
More file actions
123 lines (91 loc) · 3.52 KB
/
syntax cheatsheet.txt
File metadata and controls
123 lines (91 loc) · 3.52 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
enumerate(): works on lists, gives index number and item.
EG. for index, value in enumerate(my_list)
names = ["Alice", "Ben", "Charlie"]
for i, name in enumerate(names):
print(i, name)
0 Alice
1 Ben
2 Charlie
.items(): Works on dictionaries, gives key and value.
EG. for key, value in my_dictionary.items()
.append(): works on lists, add item to the end of a lists.
EG. current_list.append(new_list)
.update(): works on dictionaries, merges all key-value pairs from other dictionary.
EG current_dict.update(added_dict)
.sort(): used on LISTS ONLY. permenantly reorder the list
EG a_list.sort():
.sorted(): used on any iterable list,dict,set. temporarily returns a new sorted version
EG. print(sorted(a_list_or_dict))
.remove(): used to remove a specific item list,set,file etc.
EG. >>> my_list.remove('list item')
input(): lets the user type anything, and the text inside it guides what they should enter.
EG: message = input("tell me something, and i will repeat it back to you: ")
print(message)
int(): convers an input string into a numerical value
EG: >>> age = input("how old are you?)
How old are you? 21
>>> age = int(age)
>>> age >= 18
True
range(5)*: gets a range of numbers from inside parenthesis
*0,1,2,3,4 < 5 numbers total
doesnt include last number as we inc 0
range(start, stop) is left inclusive and right exclusive.
It includes 'start' but does NOT include 'stop'.
You only add +1 to 'stop' if you want the final number included.
type(): tells you what kind of value something is - string, number, list etc.
EG: >>> name = 'ben'
>>> print(type(name))
<class str>
while(): the while loop runs as long as or while, a certian condition is True
EG >>> while number <= 5
>>> print(number)
>>> number += 1
1
2
3
4
5
//Operators//
+=: add this to whats already there
EG: >>> message = "hello"
>>> message += "world"
message becomes "hello world"
%: Modulo operator: divides one number to another and gives remainder*.
EG: >>> 4 % 3
1
>>> 5 % 3
2
>>> 6 % 3
0
*if you want to tell if something is even, there wont be a remainder
Loop-control statements
break: when the loop should stop at a specific moment/statement
EG: exit loop at specific event
multiple exit conditions
simple scripts and input loops
continue: returns to the beginning of the statement, based on conditional test.
ends the current iteration of the loop
EG:
return:
Rule of thumb
boolean flag :when the loop resents an ongoing state (Active vs inactive)
EG: representing a running state
more readable logic at the top of loop
def: defines a function,
EG: >>> def greet_user():
>>> print("hello")
>>> greet_user()
__init__() method: a special method that python runs automatically whenever we create
a new instance based on the class
EG: //clean up example when better understanding//
parameter: a piece of information the function needs to do its job. in below,
username would be the parameter.
EG: >>> def greet_user(username)
>>> print("hello")
>>> greet_user()
Argument: an argument is a piece of information thats called from a function call
to a function. the argument below is bob, assigned to the paramer username
EG: >>> def greet_user(username)
>>> print(f"hello {username}")
>>> greet_user(bob)