Python Notes
Variables
Section titled “Variables”num = 5s = "Example"arr = [5, 2, 6, 2, 7]b = FalseOutput
Section titled “Output”print("Hello World") # Hello Worldprint("Hello", "Kostas") # Hello Kostas
print("num = ", num) # num = 5i = input("Enter a number: ")
# But i is not a number. It's a string. All input is initially text# We can cast this to a number
i = int(i) # Now it's a number (integer)Blocks
Section titled “Blocks”age = int(input("Enter your age: "))
if age < 18: print("You are not old enough")else: print("You are old enough")num = int(input("Enter a number: "))
if num < 0: print(f"{num} is negative") # this is called an f-stringelif num > 0: print(f"{num} is positive")else: print(f"{num} is zero")a = 5while a > 0: print(a) a -= 1 # We remove 1 from a (a = a - 1)Output:
54321for i in range(0, 10): print(i)Output:
012......89Functions
Section titled “Functions”Functions are blocks of code that can be executed whenever we want. They can take inputs and return an output
def sum(x, y): return x + y
a = sum(6, 2)print(a) # 8def method(): print("This method does not take or return values") print("It just does stuff")
method() # This is how we call it