-
Notifications
You must be signed in to change notification settings - Fork 4
Cheat Sheet English
Base form
print()
print("Hello World!") => Hello World!
Send the desired output to the console.
input()
input("Enter your name: ") => Enter your name: _ (Waiting for interactive input)
Allows receiving input from the console user for all sorts of operations.
int() / str() / float()
int("1") => 1
str(1) => "1"
float(1) => 1.0
Conversion from string to integer, integer/float to string or integer to float.
"string"[START:END:STEP]
"ABCD"[1:3] => BC
"ABCDEFG"[1:-3] => BCD
"ABCDEFGHIJKLM"[:4:-1] => MLKJIHGF
Some string slicing techniques. works for lists, tuples and sets aswell
[START::] Represents the pointer location to begin with, negative numbers are counted from the end.
[:END:] Represents the pointer location to end with, negative numbers are counted from the end.
[::STEP] Represents the number of places for a step, negative numbers are reversing the order.
"string".upper() / "string".lower()
"Python Course".upper() => PYTHON COURSE
"Python Course".lower() => python course
"wEIRD Case"[0].upper() + "wEIRD Case"[1:].lower() => Weird case
Changing the entire string into either UPPERCASE or lowercase depending on the respective function.
"string".count("")
"Hello World!".count("!") => 1
"This is an example".count("is") => 2
"wEIrD CAse StrING".lower().count("s") => 2 #Function chaining to find an occurence inside mixed-case string
Count occurences of substring inside another string.
False\ True
number1 > \ < \ >= \ <=\ == \ != number2
item in list\ string
item not in list\ string
True and False == False
True and True == True
True or False == True
Boolean values can have 2 states, either True or False. They can be used to create conditions.
if bool:
option1
elif bool:
option2
else:
default_option# condition1\condition2 are Boolean
if condition1:
this will run only if condition1 is True
elif condition2:
this will run only if condition1 is False AND condition2 is True
else:
this will run if only if condition1 AND condition2 are FalseConditions allow running certain code under certain conditions.
list.append()
list.pop()
ls = ["Tony", "Ana", "Dan", "Dvora"]
ls.append("Hanna")
print(ls)["Tony", "Ana", "Dan", "Dvora", "Hanna"]
ls = ["Tony", "Ana", "Dan", "Dvora"]
index = ls.index("Ana")
ls.pop(index)
print(ls)["Tony", "Dan", "Dvora"]
.append(object) adds the object you put in it to the end of the list as is shown.
.pop(int) pops the item at the specified index, can be used with .index() to remove a specific item from the list
list.sort()
sorted(list)
ls = [4,2,5,1,3]
ls.sort()
print(ls)[5,4,3,2,1]
ls = [4,2,5,1,3]
ls2 = sorted(ls)
print(ls2)
print(ls)[5,4,3,2,1]
[4,2,5,1,3]
When you dont need the original list order use sort(). if you do need it, use sorted() and put the sorted list in a new variable
sum(list)
min(list)
max(list)
ls = [13,4,2,51,-1,3]
max_number = max(ls)
min_number = min(ls)
sum_of_list = sum(ls)
print(max_number)
print(min_number)
print(sum_of_list)51
-1
72
these are 3 useful functions you can use on a list