Worksheet 5 - Python Introduction

From Sensors in Schools
Jump to navigation Jump to search

Printing Output

# Basic printing
print("Hello, World!")

Task: Modify the code to print a personal greeting like "Hello, [Your Name]!"

Additional Challenge: Ask the user for their name and greet them personally.

Bonus Challenge: Modify the program to greet two different names in separate lines.


Input from the User

# Basic input
name = input("Enter your name: ")
print(f"Hello {name}!")

Task: Ask the user for their age and print it.

Additional Challenge: Write a program that asks for the user’s birth year, calculates their age, and prints it.

Bonus Challenge: Modify the program to ask for the user’s favorite food and print, "Hello [name], who is [age] years old and loves [food]!"

Basic Math Operations

# Basic operations
print(5 + 3)   # Addition
print(10 - 4)  # Subtraction
print(7 * 6)   # Multiplication
print(8 / 2)   # Division

Task: Create a program that calculates the area of a rectangle (length * width).

Additional Challenge: Ask the user to input the rectangle’s length and width.

Bonus Challenge: Create a calculator that allows the user to choose the operation (addition, subtraction, etc.) they want to perform on two numbers.

Variables

# Storing and printing variables
name = "Alice"
favorite_number = 7
print(f"My name is {name} and my favorite number is {favorite_number}")

Task: Assign a new value to favorite_number and print it out.

Additional Challenge: Prompt the user for their favorite color, store it in a variable, and print a statement like "My favorite color is [color]."

Bonus Challenge: Write a small program that asks for the user’s name and age, then prints a statement with both.

Data Types

# Assigning different data types
my_string = "Hello"
my_integer = 25
my_float = 3.14

print("String:", my_string, type(my_string))
print("Integer:", my_integer, type(my_integer))
print("Float:", my_float, type(my_float))
Task: Create a new variable of type boolean (True/False) and print its type.

Additional Challenge: Write a program that uses an integer and a float and prints their sum.

Bonus Challenge: Ask the user to input a number and print its type.


Conditional Statements

# Basic conditional check
number = int(input("Enter a number: "))
if number > 0:
    print("Positive number")
elif number < 0:
    print("Negative number")
else:
    print("Zero")

Task: Modify the program to check if the number is even or odd.

Additional Challenge: Write a program to check if the input number is divisible by 5.

Bonus Challenge: Add a check to see if the number is a multiple of both 2 and 3.

Loops

# Printing numbers from 1 to 10
for i in range(1, 11):
    print(i)

Task: Print only even numbers from 1 to 10. try for i in range(0, 10, 2):

Additional Challenge: Write a loop to print numbers from 1 to 10 in reverse order. Try for i in range(10, 0, -1):

Bonus Challenge: Use a loop to calculate the sum of numbers from 1 to 20.

Lists

# Basic list and printing items
favorite_foods = ["Pizza", "Burgers", "Ice Cream"]
for food in favorite_foods:
    print(food)

Using append()

The append() method adds a single element to the end of a list.

my_list = [1, 2, 3]
my_list.append(4)
print(my_list)  # Output: [1, 2, 3, 4]

Using insert()

The insert() method allows you to add an element at a specific index.

my_list = [1, 2, 3]
my_list.insert(1, 1.5)  # Insert 1.5 at index 1
print(my_list)  # Output: [1, 1.5, 2, 3]

Using extend()

If you want to add multiple elements (from another list, for example), you can use the extend() method.

my_list = [1, 2, 3]
my_list.extend([4, 5])
print(my_list)  # Output: [1, 2, 3, 4, 5]

Using the + Operator

You can also combine lists using the + operator, but this creates a new list rather than modifying the original.

my_list = [1, 2, 3]
my_list = my_list + [4]
print(my_list)  # Output: [1, 2, 3, 4]

Using * for Unpacking

If you're using Python 3.5 or later, you can use the unpacking operator * to add elements.

my_list = [1, 2, 3]
my_list = [*my_list, 4]
print(my_list)  # Output: [1, 2, 3, 4]


Task: Add a new food item to the list and print all items.

Additional Challenge: Write a program that asks the user for their top 3 favorite movies and stores them in a list.

Bonus Challenge: Create a list of numbers, find the maximum, minimum, and average of the list, and print each.

Functions

# Basic function
def greet(name):
    print("Hello,", name, "!")
greet("Alice")


Task: Write a function that calculates the square of a number and returns the result.

Additional Challenge: Write a function to calculate the area of a triangle, given base and height.

Bonus Challenge: Write a function that takes two numbers and returns their sum, difference, product, and quotient.

String Manipulation

# Convert string to uppercase
sentence = input("Enter a sentence: ")
print("Uppercase:", sentence.upper())

Task: Print the sentence in lowercase.

Additional Challenge: Replace all spaces in the sentence with underscores using print("Modified sentence:", sentence.replace(" ", "_"))