Skip to content

Coding in Python

Task 1: print() function

print("Hello, World!")

print("You can put any text here!")

Task 2: Variables

name = "Sarah"

age = 14

is_student = True

Task 3: Display variables

print(name)
print(age)
print(is_student)

Task 4: Display variable with string

print("My name is " + name)

Task 5: Arithmetic operations

#Addition
print(5 + 3)

#Subtraction
print(10  4)

#Multiplication
print(7 * 2)

#Division
print(8 / 2)

#Modulus (remainder)
print(9 % 4)

Task 6: String Operations

#Concatenation

first_name = "First"
last_name = "Last"
full_name = first_name + " " + last_name  
print(full_name)

#Repetition

laugh = "Ha" * 3
print(laugh)

Task 7: Import module

import random

Task 8: Set variables

# The computer picks a random number between 1 and 10
number_to_guess = random.randint(1, 10)

# Variable to store the user's guess
guess = 0

Task 9: While loop and if statement

print("Guess the number:")

# Loop until the user guesses the correct number

while guess != number_to_guess:
    guess = int(input())           # Get user input and convert it to integer
    if guess < number_to_guess:    # If user guess is lower than the number
        print("Too low! Try again:")
    elif guess > number_to_guess:  # If user guess is higher than the number
        print("Too high! Try again:")
    else:                          # not lower & not higher? guess is correct
        print("Congratulations! You guessed it!")