name = "Alexander"
print(f"Hello, {name}!")Hello, Alexander!
Useful commands and tips
x = 10 name = "Alexander" # String variable
flags = 0 # Integer variable
butterflies = True # Boolean variabletype() to check the type of a variable.int(): Converts to integer.float(): Converts to float.str(): Converts to string.bool(): Converts to boolean.+.f"..." for formatted strings.name = "Alexander"
print(f"Hello, {name}!")Hello, Alexander!
| Symbol | Meaning | Example |
|---|---|---|
| == | Equal to | score == 100 |
| != | Not equal to | degree != “Computer Science” |
| < | Less than | salary < 80000 |
| > | Greater than | experience > 5 |
| <= | Less than or equal to | age <= 65 |
| >= | Greater than or equal to | test_score >= 80 |
| Symbol | Meaning | Example |
|---|---|---|
| and | Both conditions must be true | score > 80 and experience > 5 |
| or | At least one condition must be true | score > 80 or experience > 5 |
| not | Condition must be false | not (score > 80) |
if condition:
# code to execute if condition is Trueflat_rating = 8
if flat_rating >= 7:
print("This is a good apartment!")This is a good apartment!
if condition:
# code to execute if condition is True
else:
# code to execute if condition is Falseflat_rating = 4
if flat_rating >= 7:
print("Apply for this flat!")
else:
print("Keep searching!")Keep searching!
if condition:
# code to execute if condition is True
elif condition:
# code to execute if condition is False
else:
# code to execute if condition is Falseflat_rating = 8
if flat_rating >= 9:
print("Amazing flat - apply immediately!")
elif flat_rating >= 7:
print("Good flat - consider applying")
else:
print("Keep looking")Good flat - consider applying
and, or, not.if (condition1) and (condition2):
# code if both conditions are True
elif (condition1) or (condition2):
# code if at least one condition is True
else:
# code if none of the conditions are Trueflat_rating = 9
price = 900
if (flat_rating >= 9) and (price < 1000):
print("Amazing flat - apply immediately!")Amazing flat - apply immediately!
[].ratings = [4.5, 3.8, 4.2]
restaurants = ["Magic Place", "Sushi Bar", "Coffee Shop"][index] to access elements.print(restaurants[0]) # Access the first elementMagic Place
[-1] to access the last element.print(restaurants[-1]) # Access the last elementCoffee Shop
[start:end] to access a range of elements.print(restaurants[0:2]) # Access the first two elements['Magic Place', 'Sushi Bar']
append() to add an element to the end of the list.restaurants.append("Pasta Place")insert() to add an element at a specific index.restaurants.insert(0, "Pasta Magic")remove() to remove an element by value.restaurants.remove("Pasta Place")pop() to remove an element by index.restaurants.pop(0)'Pasta Magic'
restaurant_data = [
["Pasta Place", 4.5, 3],
["Sushi Bar", 4.2, 1]
]
print(restaurants[0][1]) # Access the second element of the first lista
().ratings = (4.5, 3.8, 4.2)
restaurant_info = ("Pasta Place", "Italian", 2020)for item in sequence:
# code to execute for each itemtreatments = ["Standard Drug", "New Drug A", "New Drug B"]
for treatment in treatments:
print(f"Evaluating efficacy of {treatment}")Evaluating efficacy of Standard Drug
Evaluating efficacy of New Drug A
Evaluating efficacy of New Drug B
range(start, stop, step)for phase in range(5): # 0 to 4
print(f"Starting Phase {phase + 1}")Starting Phase 1
Starting Phase 2
Starting Phase 3
Starting Phase 4
Starting Phase 5
for phase in range(1, 5): # 1 to 4
print(f"Starting Phase {phase}")Starting Phase 1
Starting Phase 2
Starting Phase 3
Starting Phase 4
for phase in range(1, 5, 2): # 1 to 4, step 2
print(f"Starting Phase {phase}")Starting Phase 1
Starting Phase 3
efficacy_scores = [45, 60, 75, 85, 90]
for score in efficacy_scores:
if score < 50:
continue
print(f"Treatment efficacy: {score}%")
if score >= 85:
breakrestaurant_info = ("Pasta Place", "Italian", 2020)
name, cuisine, year = restaurant_info
print(name)
print(cuisine)
print(year)Pasta Place
Italian
2020
while condition:
# code to execute while condition is Truephase = 1
while phase <= 5:
print(f"Starting Phase {phase}")
phase += 1Starting Phase 1
Starting Phase 2
Starting Phase 3
Starting Phase 4
Starting Phase 5
def keyword.def function_name(parameters):
# code to execute (function body)
return value # Optionaldef greet_visitor(name):
return f"Welcome to the library, {name}!"
greet_visitor("Student")'Welcome to the library, Student!'
def multiply_by_two(number):
return number * 2
result = multiply_by_two(5)
print(result)10
None.def greet_visitor(name="People"):
return f"Welcome to the library, {name}!"
print(greet_visitor()) # Calls the function with the default parameter
print(greet_visitor("Tobias")) # Calls the function with a custom parameterdef greet_visitor(name, age):
return f"Welcome to the library, {name}! You are {age} years old."
print(greet_visitor("Tobias", 30))string.method().strip() - Removes whitespace from start and end.title() - Capitalizes first letter of each word.lower() - Converts to lowercase.upper() - Converts to uppercasetitle = "the hitchhikers guide"
print(title.title())The Hitchhikers Guide
title = " the hitchhikers guide "
print(title.strip())the hitchhikers guide
import.import math
import randomas keyword to create an alias.import random as rdrandom_number = rd.random()
print(random_number)0.25677978942073065
uv.uv add package_name
conda instead.conda install package_name