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

Python

The document contains several Python code snippets demonstrating basic programming concepts such as user input, arithmetic operations, conditional statements, loops, and a simple calculator. It includes examples for taking user input, adding two numbers, checking if a number is even or odd, finding the largest of two numbers, and summing the first N numbers. Each section provides a clear function with corresponding print statements to display results.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Python

The document contains several Python code snippets demonstrating basic programming concepts such as user input, arithmetic operations, conditional statements, loops, and a simple calculator. It includes examples for taking user input, adding two numbers, checking if a number is even or odd, finding the largest of two numbers, and summing the first N numbers. Each section provides a clear function with corresponding print statements to display results.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Taking User Input

name = input("Enter your name: ")


print("Hello", name)

Add Two Numbers


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

sum = a + b
print("Sum is:", sum)

Check Even or Odd


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

if num % 2 == 0:
print("Even number")
else:
print("Odd number")

Find Largest of Two Numbers


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

if a > b:
print("Largest is:", a)
else:
print("Largest is:", b)

Simple Calculator
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

print("1. Add 2. Subtract 3. Multiply 4. Divide")


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

if choice == 1:
print("Result:", a + b)
elif choice == 2:
print("Result:", a - b)
elif choice == 3:
print("Result:", a * b)
elif choice == 4:
print("Result:", a / b)
else:
print("Invalid choice")

Simple Loop (Print 1 to 5)


for i in range(1, 6):
print(i)

Sum of First N Numbers


n = int(input("Enter a number: "))
sum = 0

for i in range(1, n+1):


sum += i

print("Sum is:", sum)

You might also like