0% found this document useful (0 votes)
8 views

Python Programming Worksheet

The document is a Python programming worksheet covering various topics including data types, variables, print functions, input functions, and conditional statements. It includes questions, code examples, and predictions regarding the output of specific code snippets. Additionally, it features a bonus challenge related to user input for voting eligibility.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Python Programming Worksheet

The document is a Python programming worksheet covering various topics including data types, variables, print functions, input functions, and conditional statements. It includes questions, code examples, and predictions regarding the output of specific code snippets. Additionally, it features a bonus challenge related to user input for voting eligibility.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Python Programming Worksheet

Section 1: Data Types & Variables

1. What are the four main data types in Python? Give one example of each.

2. Write a Python script to declare the following variables:

o name (string type)

o age (integer type)

o height (float type)

o is_student (boolean type)

3. Predict the output of the following code:

x=5

y = 2.5

z = "Hello"

print(type(x), type(y), type(z))

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.

Section 2: Print Function

6. Write a Python program to print the following:

Welcome to Python Programming!

Today's topic is Variables and Data Types.

7. What is the difference between:

print("Hello", "World")
print("Hello" + "World")

8. Format the following print statement to include the variable in the message:

name = "Alice"

print("Hello, " + name + ". Welcome!")

9. Write a script that uses an f-string to print:

My name is [name]. I am [age] years old.

(Replace [name] and [age] with actual variable values.)

10. Predict the output of:

print("5 + 2 =", 5 + 2)

print("5" + "2")

Section 3: Input Function

11. Write a program that asks the user for their name and prints:

Hello, [name]! Welcome to Python Programming.

12. Write a script that takes two numbers as input and prints their sum.

13. Modify the script from Q12 to handle decimal numbers.

14. Predict the output of:

x = input("Enter a number: ")

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

Section 4: Conditional Statements (if...else)

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

Marks >= 80: Grade B

Marks >= 70: Grade C

Marks >= 60: Grade D

Marks < 60: Grade F

19. Predict the output of the following code:

num = 10

if num > 5:

print("Greater than 5")

else:

print("5 or less")

20. Write a program that takes a number as input and checks:

o If the number is divisible by 3, print "Fizz."

o If the number is divisible by 5, print "Buzz."

o If divisible by both, print "FizzBuzz."

o Otherwise, print the number.

Bonus Challenge

Write a program that asks the user for their name and age. If their age is above 18, print:

Hello, [name]. You are eligible to vote!

Otherwise, print:

Sorry, [name]. You are not eligible to vote.


Section 1: Data Types & Variables

1. Main Data Types in Python:

o int: Integer values, e.g., age = 25

o float: Decimal numbers, e.g., height = 5.8

o str: Strings of text, e.g., name = "Alice"

o bool: Boolean values, e.g., is_student = True

2. Script to Declare Variables:

python

CopyEdit

name = "John"

age = 20

height = 5.9

is_student = True

3. Output of the Code:

arduino

CopyEdit

<class 'int'> <class 'float'> <class 'str'>

4. Code Explanation: Output will be:

CopyEdit

Ten

Python allows dynamic typing, so num can hold both integers and strings.

5. Swapping Two Variables Without a Third Variable:

python

CopyEdit

a, b = 5, 10

a, b = b, a
print(a, b) # Output: 10, 5

Section 2: Print Function

6. Print Statement:

python

CopyEdit

print("Welcome to Python Programming!")

print("Today's topic is Variables and Data Types.")

7. Difference Between the Two Statements:

o print("Hello", "World") adds a space between "Hello" and "World".

o print("Hello" + "World") concatenates them without a space, resulting in


HelloWorld.

8. Formatted Message:

python

CopyEdit

name = "Alice"

print("Hello, " + name + ". Welcome!")

9. f-String Example:

python

CopyEdit

name = "Alice"

age = 25

print(f"My name is {name}. I am {age} years old.")

10. Output of the Code:

CopyEdit

5+2=7
52

Section 3: Input Function

11. Greeting Program:

python

CopyEdit

name = input("What is your name? ")

print(f"Hello, {name}! Welcome to Python Programming.")

12. Sum of Two Numbers:

python

CopyEdit

num1 = int(input("Enter the first number: "))

num2 = int(input("Enter the second number: "))

print(f"The sum is: {num1 + num2}")

13. Handle Decimal Numbers:

python

CopyEdit

num1 = float(input("Enter the first number: "))

num2 = float(input("Enter the second number: "))

print(f"The sum is: {num1 + num2}")

14. Predict Output:

arduino

CopyEdit

<class 'str'>

Input is always a string unless explicitly converted.

15. Check Age:


python

CopyEdit

age = int(input("Enter your age: "))

if age > 18:

print("Adult")

else:

print("Minor")

Section 4: Conditional Statements (if...else)

16. Odd or Even Program:

python

CopyEdit

num = int(input("Enter a number: "))

if num % 2 == 0:

print("Even")

else:

print("Odd")

17. Check Number Sign:

python

CopyEdit

num = int(input("Enter a number: "))

if num > 0:

print("Positive")

elif num < 0:

print("Negative")

else:
print("Zero")

18. Student Grade:

python

CopyEdit

marks = int(input("Enter marks: "))

if marks >= 90:

print("Grade A")

elif marks >= 80:

print("Grade B")

elif marks >= 70:

print("Grade C")

elif marks >= 60:

print("Grade D")

else:

print("Grade F")

19. Output Prediction:

mathematica

CopyEdit

Greater than 5

20. FizzBuzz Program:

python

CopyEdit

num = int(input("Enter a number: "))

if num % 3 == 0 and num % 5 == 0:

print("FizzBuzz")

elif num % 3 == 0:
print("Fizz")

elif num % 5 == 0:

print("Buzz")

else:

print(num)

Bonus Challenge

Eligibility to Vote:

python

CopyEdit

name = input("What is your name? ")

age = int(input("What is your age? "))

if age > 18:

print(f"Hello, {name}. You are eligible to vote!")

else:

print(f"Sorry, {name}. You are not eligible to vote.")

You might also like