Python Programming Worksheet
Python Programming Worksheet
1. What are the four main data types in Python? Give one example of each.
x=5
y = 2.5
z = "Hello"
4. What will happen if you try to change the type of a variable in Python?
Example:
num = 10
num = "Ten"
print(num)
5. Write code to swap two variables a and b without using a third variable.
print("Hello", "World")
print("Hello" + "World")
8. Format the following print statement to include the variable in the message:
name = "Alice"
print("5 + 2 =", 5 + 2)
print("5" + "2")
11. Write a program that asks the user for their name and prints:
12. Write a script that takes two numbers as input and prints their sum.
print(type(x))
15. Write a program that asks the user for their age and checks if they are above 18. Print
"Adult" if true, otherwise "Minor."
16. Write a program to check if a number entered by the user is odd or even.
17. Write a program to check if the input number is positive, negative, or zero.
18. Create a Python program to determine the grade of a student based on their marks:
Marks >= 90: Grade A
num = 10
if num > 5:
else:
print("5 or less")
Bonus Challenge
Write a program that asks the user for their name and age. If their age is above 18, print:
Otherwise, print:
python
CopyEdit
name = "John"
age = 20
height = 5.9
is_student = True
arduino
CopyEdit
CopyEdit
Ten
Python allows dynamic typing, so num can hold both integers and strings.
python
CopyEdit
a, b = 5, 10
a, b = b, a
print(a, b) # Output: 10, 5
6. Print Statement:
python
CopyEdit
8. Formatted Message:
python
CopyEdit
name = "Alice"
9. f-String Example:
python
CopyEdit
name = "Alice"
age = 25
CopyEdit
5+2=7
52
python
CopyEdit
python
CopyEdit
python
CopyEdit
arduino
CopyEdit
<class 'str'>
CopyEdit
print("Adult")
else:
print("Minor")
python
CopyEdit
if num % 2 == 0:
print("Even")
else:
print("Odd")
python
CopyEdit
if num > 0:
print("Positive")
print("Negative")
else:
print("Zero")
python
CopyEdit
print("Grade A")
print("Grade B")
print("Grade C")
print("Grade D")
else:
print("Grade F")
mathematica
CopyEdit
Greater than 5
python
CopyEdit
print("FizzBuzz")
elif num % 3 == 0:
print("Fizz")
elif num % 5 == 0:
print("Buzz")
else:
print(num)
Bonus Challenge
Eligibility to Vote:
python
CopyEdit
else: