= "Alexander"
name print(f"Hello, {name}!")
Hello, Alexander!
Useful commands and tips
x = 10
= "Alexander" # String variable
name = 0 # Integer variable
flags = True # Boolean variable butterflies
type()
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.= "Alexander"
name 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 True
= 8
flat_rating 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 False
= 4
flat_rating 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 False
= 8
flat_rating 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 True
= 9
flat_rating = 900
price if (flat_rating >= 9) and (price < 1000):
print("Amazing flat - apply immediately!")
Amazing flat - apply immediately!
[]
.= [4.5, 3.8, 4.2]
ratings = ["Magic Place", "Sushi Bar", "Coffee Shop"] restaurants
[index]
to access elements.print(restaurants[0]) # Access the first element
Magic Place
[-1]
to access the last element.print(restaurants[-1]) # Access the last element
Coffee 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."Pasta Place") restaurants.append(
insert()
to add an element at a specific index.0, "Pasta Magic") restaurants.insert(
remove()
to remove an element by value."Pasta Place") restaurants.remove(
pop()
to remove an element by index.0) restaurants.pop(
'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 list
a
()
.= (4.5, 3.8, 4.2)
ratings = ("Pasta Place", "Italian", 2020) restaurant_info
for item in sequence:
# code to execute for each item
= ["Standard Drug", "New Drug A", "New Drug B"]
treatments 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
= [45, 60, 75, 85, 90]
efficacy_scores for score in efficacy_scores:
if score < 50:
continue
print(f"Treatment efficacy: {score}%")
if score >= 85:
break
= ("Pasta Place", "Italian", 2020)
restaurant_info = restaurant_info
name, cuisine, year print(name)
print(cuisine)
print(year)
Pasta Place
Italian
2020
while condition:
# code to execute while condition is True
= 1
phase while phase <= 5:
print(f"Starting Phase {phase}")
+= 1 phase
Starting 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 # Optional
def greet_visitor(name):
return f"Welcome to the library, {name}!"
"Student") greet_visitor(
'Welcome to the library, Student!'
def multiply_by_two(number):
return number * 2
= multiply_by_two(5)
result 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 parameter
def 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 uppercase= "the hitchhikers guide"
title print(title.title())
The Hitchhikers Guide
= " the hitchhikers guide "
title print(title.strip())
the hitchhikers guide
import
.import math
import random
as
keyword to create an alias.import random as rd
= rd.random()
random_number print(random_number)
0.39766127791648553
pip
.pip install package_name
conda
instead.conda install package_name