[go: up one dir, main page]

0% found this document useful (0 votes)
6 views4 pages

Assignment 02

The document outlines an assignment with three questions focusing on variables, operators, and loops in programming. It includes user input handling for different data types, performing arithmetic and logical operations, and processing a list of numbers using loops with control statements. Each section provides code examples demonstrating the objectives and expected outputs.

Uploaded by

Areeba Gul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Assignment 02

The document outlines an assignment with three questions focusing on variables, operators, and loops in programming. It includes user input handling for different data types, performing arithmetic and logical operations, and processing a list of numbers using loops with control statements. Each section provides code examples demonstrating the objectives and expected outputs.

Uploaded by

Areeba Gul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment 02

Question 1: Variables and Data Types


Objective:
 Take input from the user for different data types.

 Perform simple operations and output the results.

# Accepting user input

user_string = input("Enter a string: ") # String input

user_int = int(input("Enter an integer: ")) # Integer input

user_float = float(input("Enter a float: ")) # Float input

user_bool = input("Enter a boolean (True/False): ").capitalize() == "True" #


Convert string to Boolean

Explanation:

 input() takes user input as a string

 int() and float() convert that input to numbers

 .capitalize() == "True" converts "true"/"True" to True, and others to


False

# Initializing variables

string_var = user_string

int_var = user_int

float_var = user_float

bool_var = user_bool
# Printing original variables

print("\nOriginal variables:")

print(f"String: {string_var}")

print(f"Integer: {int_var}")

print(f"Float: {float_var}")

print(f"Boolean: {bool_var}")

Operations:
# Converting string to uppercase

uppercase_string = string_var.upper()
print(f"\nUppercase String: {uppercase_string}")

# Checking if integer is even or odd

if int_var % 2 == 0:
print(f"The number {int_var} is Even")
else:
print(f"The number {int_var} is Odd")

# Multiplying float by 2

doubled_float = float_var * 2
print(f"Doubled float: {doubled_float}")

……………………………………………………………………………………………………………
…..

Question 2: Operators
Objective:
 Perform arithmetic, comparison, and logical operations between two numbers.

# Accepting two numbers from user

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


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

Arithmetic Operations:
print(f"\nAddition: {num1 + num2}")
print(f"Subtraction: {num1 - num2}")
print(f"Multiplication: {num1 * num2}")
print(f"Division: {num1 / num2}")
print(f"Modulus: {num1 % num2}")
print(f"Floor Division: {num1 // num2}")

Comparison and Logical Operations:


print(f"\nFirst number is greater than second: {num1 > num2}")
print(f"First number is equal to second: {num1 == num2}")

# Logical operations
condition1 = num1 > num2
condition2 = num2 < 10
print(f"Both conditions are true: {condition1 and condition2}")

……………………………………………………………………………………………………………

Question 3: Loops
Objective:
 Use a loop to process a list of numbers

 Use break, continue, and condition checks

# Accepting list of integers from user


input_numbers = input("Enter a list of numbers separated by spaces: ")

numbers = [int(num) for num in input_numbers.split()]

Looping through the list:


loop_ended_naturally = True

for num in numbers:

if num == 20:

print("Breaking at 20")
loop_ended_naturally = False

break

if num > 10:

print(f"Skipping {num}")

continue

print(num)

if loop_ended_naturally:

print("Loop ended naturally")

……………………………………………………………………………………………………………

You might also like