Intro to Python

Display in console.

Type print('Hello World') to display in the console.

Assigning variables

my_variable="6"
print("my number is " + my_variable + " and I love it")
my_variable="10"
print("my new number is " + my_variable + " and I still love it")

Datatypes

string "apple"
integer 1 3 
float 1.3 9.369
boolean True/False

Use snake_case in Python.

To type an apostrophe, you need to escape it /'

type() will give you the type of your variable.

int() turns into integer, float() for float, str()

Can't turn a numerical int into a float, you need to do this: int(float("3.4))

Arithmetic

print('Addition : ', a + b)
print('Subtraction : ', a - b)
print('Multiplication : ', a * b)
print('Division (float) : ', a / b)
print('Division (floor) : ', a // b)
print('Modulus : ', a % b)
print('Exponent : ', a ** b)

Strings

string="hello world"
striing.upper()
string.lower()
string.capitalize() # First letter caps
string.title() # first letter of each word caps
string.len() # length of the string
string.count("o") # counts the number of "o" in the centence
string[0] # index of the character in the string
string[2:] # from index 2 till the end
string[2:15] #between both indexes (not inclusive of the last index)
string.find("something")
string.repace("something", "something else") # doesn't mutate, only returns.
"something" in string # boolean if "something" exists in the string
"something" not in string
message=f"{name} loves the color {color.lower()}!" # use variables within a string

User input

name = input("what's your name")
print("Hello {name}")

Numbers

int("6.4") # reutnrs 6
float("6.4") # returns 6.4
round("6.4") # rounds to 6

Lists

ducks = ["riri", "Fifi", "Loulou"]
print(ducks[0], list[3])
print(ducksst[3:7]
pring(ducksst[:])
print(ducksist.index("Fifi")
print(ducks.count("LouLou")'
print(ducks.sort(reverse=True))
print(min(ducks))
print(max(ducks))
ducks.append("Tata")
ducks.insert(1, "Tata")
ducks[3]="Tata" # Replaces the item at index 3
ducks.extend(other_list) # 
ducks.remove()
ducks.pop(2) # 
ducks.clear() #empties the list
del(ducks) #deletes the whole variable
new_list= ducks[:]
new_list= ducks.copy()
new_list=list(ducks)
msg="Hello World"
print(msg.split()) # splits each word into a list
print(msg.split("w")) # splits string at each letter w cgreates a list

Last updated

Was this helpful?