Worksheet 5 - Python Introduction

From Sensors in Schools
Revision as of 08:04, 27 October 2024 by EdmondLascaris (talk | contribs) (Created page with "=Printing Output= <syntaxhighlight lang="python"> # Basic printing print("Hello, World!") </syntaxhighlight> 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= python Copy code # Basic input name = input("Enter your name: ") print("Hello,", name, "!") Task: As...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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

python Copy code

  1. Basic input

name = input("Enter your name: ") print("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

python Copy code

  1. 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.

3. Variables python Copy code

  1. Storing and printing variables

name = "Alice" favorite_number = 7 print("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.

4. Data Types python Copy code

  1. 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.


6. Conditional Statements python Copy code

  1. 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.

7. Loops python Copy code

  1. Printing numbers from 1 to 10

for i in range(1, 11):

   print(i)

Task: Print only even numbers from 1 to 10.

Additional Challenge: Write a loop to print numbers from 1 to 10 in reverse order.

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

8. Lists python Copy code

  1. Basic list and printing items

favorite_foods = ["Pizza", "Burgers", "Ice Cream"] for food in favorite_foods:

   print(food)

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.

9. Functions python Copy code

  1. 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.

10. String Manipulation python Copy code

  1. 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.

Bonus Challenge: Write a program that asks for a word and checks if it’s a palindrome (same forwards and backwards).