0% found this document useful (0 votes)
5 views2 pages

Python Conditional Statements

The document contains Python code snippets demonstrating basic operations such as multiplication, addition, and printing names. It also includes a simple calculator program that prompts the user for two numbers and an operation, performing the specified calculation while handling potential errors. The code checks for valid operations and provides appropriate messages for invalid inputs or division by zero.

Uploaded by

Khinsandar Naing
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)
5 views2 pages

Python Conditional Statements

The document contains Python code snippets demonstrating basic operations such as multiplication, addition, and printing names. It also includes a simple calculator program that prompts the user for two numbers and an operation, performing the specified calculation while handling potential errors. The code checks for valid operations and provides appropriate messages for invalid inputs or division by zero.

Uploaded by

Khinsandar Naing
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

# 1.

Print 2*3

print(2 * 3)

# 2. Print a blank line

print()

# 3. Adding two numbers

a=5

b=7

sum_ab = a + b

print("Sum of", a, "and", b, "is", sum_ab)

# 4. Print first name and last name

first_name = "John"

last_name = "Doe"

print("Full Name:", first_name, last_name)

# 5. Simple calculator program

print("\nSimple Calculator")

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

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

# Check if first number is less than second number

if num1 < num2:

print("First number is less than second number. Please try again.")

else:

print("Choose operation: add, sub, multiply, division, remainder, power, exit")

operation = input("Enter operation: ").lower() # convert to lowercase for easy matching


if operation == "add":

print("Result:", num1 + num2)

elif operation == "sub":

print("Result:", num1 - num2)

elif operation == "multiply":

print("Result:", num1 * num2)

elif operation == "division":

if num2 != 0:

print("Result:", num1 / num2)

else:

print("Cannot divide by zero!")

elif operation == "remainder":

if num2 != 0:

print("Result:", num1 % num2)

else:

print("Cannot divide by zero!")

elif operation == "power":

print("Result:", num1 ** num2)

elif operation == "exit":

print("Exiting program")

else:

print("Invalid operation")

You might also like