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

Python-Programming-Assignment-02

The document contains three programming assignments in Python. The first assignment involves accepting various data types from the user and performing operations like converting a string to uppercase and checking if an integer is even or odd. The second assignment focuses on arithmetic and logical operations between two numbers, while the third assignment requires looping through a list of integers with conditions to skip numbers greater than 10 and stop at 20.

Uploaded by

fazalabbas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python-Programming-Assignment-02

The document contains three programming assignments in Python. The first assignment involves accepting various data types from the user and performing operations like converting a string to uppercase and checking if an integer is even or odd. The second assignment focuses on arithmetic and logical operations between two numbers, while the third assignment requires looping through a list of integers with conditions to skip numbers greater than 10 and stop at 20.

Uploaded by

fazalabbas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment # 02

Question 1: Variables and Data Types

Problem: Write a Python program that:

1. Accepts a string, an integer, a float, and a boolean from the user.


2. Initializes variables for each type, and prints them out.
3. Convert the string to uppercase and print it.
4. Check if the integer is even or odd and print the result.
5. Multiply the float by 2 and print the result.

Solution of Question # 1
# Question 1 Solution
#This program is about taking input of differenet types

#Taking inputs from the users


string_var = input("Enter a String: ")
integer_var = input("Enter a Integer: ")
float_var = input("Enter a Float: ")
bool_var = input("Enter True or False: ")

#Converting the variables accordirng to they theirs types


integer_var = int(integer_var)
float_var = float(float_var)

#Print all the variables


print(f"String Variable: ", string_var)
print(f"Integer Variable: ", integer_var)
print(f"Float Variable: ", float_var)
print(f"Boolean Variable: ", bool_var)

# Converting string to uppercase


print(f"String Variable in uppercase: {string_var.upper()}")

# Checking the integer is even or not


if integer_var%2==0:
print(f"Integer Variable is even: {integer_var}")
elif integer_var!=0:
print(f"Integer Variable is odd: {integer_var}")
else:
print(f"Integer Variable type is undetermined: {integer_var}")

#Muliplying the float with 2


print(f"Float {float_var} after multiplying with 2 is {float_var*2} ")

Output:
Enter a String: amd
Enter a Integer: 9
Enter a Float: 2.5
Enter True or False: True
String Variable: amd
Integer Variable: 9
Float Variable: 2.5
Boolean Variable: True
String Variable in uppercase: AMD
Integer Variable is odd: 9
Float 2.5 after multiplying with 2 is 5.0

Question 2: Operators
Problem: Write a Python program that:

1. Accepts two numbers as input from the user.


2. Performs and prints the result of all the arithmetic operations (addition, subtraction,
multiplication, division, modulus, flow division) between these two numbers.
3. Use comparison operators to check if the first number is greater than the second,
and if they are equal.
4. Use logical operators to combine two conditions (e.g., the first number is greater
than the second, and the second number is less than 10).

# Question 2 Solution
# Airthmetic and Logical Operators

#Taking inputs from the users


number1 = input("Enter a Number1: ")
number2 = input("Enter a Number2: ")

#Converting the variables accordirng to they theirs types


number1 = int(number1)
number2 = int(number2)

#Arithmetic Operations
print(f"Addition of {number1} + {number2} = {number1+number2}")
print(f"Substraction of {number1} - {number2} = {number1-number2}")
print(f"Multipltion of {number1} * {number2} = {number1*number2}")
print(f"Division of {number1} / {number2} = {number1/number2}")
print(f"Modulus of {number1} % {number2} = {number1%number2}")
print(f"Flow Division of {number1} // {number2} = {number1//number2}")

#Comparing two numbes


if number1 > number2:
print(f"{number1} is greater than {number2}")
elif number2 > number1:
print(f"{number2} is greater than {number1}")
else:
print(f"{number1} and {number2} comparison cannot be performed")

#Checking that number1 is greater than number2 and second number is less that
10 or not.
#Comparing two numbes
if number1 > number2 and number2 < 10:
print(f"{number1} is greater than {number2} and {number2} is less than 10")
elif number2 > number1 and number2 >=10:
print(f"{number1} is greater than {number2} and {number2} is equal or
greater than 10")
elif number1 < number2 and number2 < 10:
print(f"{number1} is less than {number2} and {number2} is less than 10")
elif number2 < number1 and number2 >=10:
print(f"{number1} is less than {number2} and {number2} is equal or greater
than 10")
else:
print(f"Invalid Operation!")
Output:
Enter a Number1: 3
Enter a Number2: 4
Addition of 3 + 4 = 7
Substraction of 3 - 4 = -1
Multipltion of 3 * 4 = 12
Division of 3 / 4 = 0.75
Modulus of 3 % 4 = 3
Flow Division of 3 // 4 = 0
4 is greater than 3
3 is less than 4 and 4 is less than 10
Question 3: Loops

Problem: Write a Python program that:

1. Accepts a list of integers from the user.


2. Loops through the list and prints out each number.
3. If a number is greater than 10, skip it using the continue statement.
4. Stop the loop if the number is 20 using the break statement.
5. After the loop ends, print a message that the loop ended naturally.

Solution of Question # 3
# Question 3 Solution

# List Operations

#Taking input from usre


Integer_list = map(int, input("Enter number separted by spaces: ").split("
"))

#Converting the variables accordirng to they theirs types


Integer_list = list(Integer_list)

#Performing Loop to go through the list


for num in Integer_list:

#Performing logical operations


if num > 10 and num!=20:
print(f"Skipping {num}")
continue
elif num ==20:
print(f"Breaking at {num}")
break
else:
print(num)

#Indication that loop is ending successfully


print("Loops end")

Output:
Enter number separted by spaces: 10 5 20 5 6 5 1 2 3
10
5
Breaking at 20 Loops end

You might also like