x = 5
name = "Alex"
print(x)
print(name)
_).myVar and myvar point to different objects).if, else, for) cannot be used as identifiers.Valid Identifiers:
age = 21
_colour = "lilac"
total_score = 90
=.y = x binds y to the same memory object referenced by x.Experiment with variables and dynamic typing:
A question is generated for each topic below. Answer it and get instant AI feedback, or generate a new one.
Operators are special symbols or keywords used to perform specific mathematical, relational, or logical computations on values and variables.
Used to perform standard mathematical calculations.
+ (Addition): Adds values on either side of the operator.- (Subtraction): Subtracts right operand from left operand.* (Multiplication): Multiplies values./ (Division): Divides left operand by right operand (returns a float).// (Floor Division): Division that results in a whole number adjusted down to the nearest integer.% (Modulus): Divides left operand by right operand and returns the remainder.** (Exponent): Performs exponential (power) calculation on operators.print(10 / 3) # Output: 3.3333333333333335
print(10 // 3) # Output: 3
print(2 ** 3) # Output: 8
Used to compare values, yielding a boolean outcome (True or False).
== (Equal to)!= (Not equal to)> (Greater than)< (Less than)>= (Greater than or equal to)<= (Less than or equal to)Used to combine conditional evaluations.
and: Returns True if both operands are true.or: Returns True if at least one operand is true.not: Reverses the boolean state of its operand.x = 5
print(x > 3 and x < 10) # Output: True
Experiment with operators:
A question is generated for each topic below. Answer it and get instant AI feedback, or generate a new one.
Control flow lets your program make decisions and run different code based on conditions.
Comparison operators compare values and produce a boolean result (True or False).
== (equal to), != (not equal to)> (greater than), < (less than)>= (greater than or equal to), <= (less than or equal to)age = 18
print(age >= 18) # Output: True
print(age == 21) # Output: False
and: True only if both operands are true.or: True if at least one operand is true.not: Reverses the boolean value.score = 85
print(score > 50 and score < 100) # Output: True
print(not score > 90) # Output: True
Python evaluates conditions top to bottom and runs the first block whose condition is true.
temperature = 30
if temperature > 35:
print("Very hot")
elif temperature > 20:
print("Warm")
else:
print("Cool")
IndentationError.You can place an if inside another if to check multiple levels of conditions.
age = 25
has_id = True
if age >= 18:
if has_id:
print("Entry allowed")
else:
print("ID required")
In conditions, some values are treated as False (falsy) and most others as True (truthy).
0, 0.0, "", [], None, False.True.name = ""
if name:
print("Hello, " + name)
else:
print("No name given")
Experiment with conditionals:
A question is generated for each topic below. Answer it and get instant AI feedback, or generate a new one.
Loops let you repeat a block of code many times, which is essential for working with collections and repetitive tasks.
A for loop iterates over each item in a sequence (a range, list, or string).
for fruit in ["apple", "banana", "cherry"]:
print(fruit)
range() produces a sequence of numbers to loop over.
range(stop): 0 to stop-1.range(start, stop): start to stop-1.range(start, stop, step): with a custom step.for i in range(3):
print(i) # 0 1 2
for i in range(1, 6, 2):
print(i) # 1 3 5
A while loop repeats as long as its condition is true. Be careful to update the condition so it eventually becomes false.
count = 0
while count < 3:
print(count)
count += 1
break: exits the loop immediately.continue: skips the rest of the current iteration and moves to the next.pass: does nothing (a placeholder for an empty block).for i in range(10):
if i == 3:
continue # skip 3
if i == 6:
break # stop at 6
print(i) # 0 1 2 4 5
You can place a loop inside another loop. The inner loop runs fully for each iteration of the outer loop.
for row in range(2):
for col in range(3):
print(row, col)
total += x.enumerate() to get both index and value.total = 0
for n in [2, 4, 6]:
total += n
print(total) # Output: 12
for idx, val in enumerate(["a", "b"]):
print(idx, val) # 0 a / 1 b
Experiment with loops:
A question is generated for each topic below. Answer it and get instant AI feedback, or generate a new one.