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

python programs-1

This document provides a comprehensive tutorial on basic Python operations, including arithmetic operations, checking even or odd numbers, determining leap years, calculating factorials, and checking for palindromes. Each section includes step-by-step instructions, code examples, and key takeaways to help users understand and implement these concepts in Python. The document emphasizes user input handling, conditional statements, and the use of various Python functions.

Uploaded by

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

python programs-1

This document provides a comprehensive tutorial on basic Python operations, including arithmetic operations, checking even or odd numbers, determining leap years, calculating factorials, and checking for palindromes. Each section includes step-by-step instructions, code examples, and key takeaways to help users understand and implement these concepts in Python. The document emphasizes user input handling, conditional statements, and the use of various Python functions.

Uploaded by

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

an

ith
ad
ig
ita
l
1.Basic Operations in Python: Add,
Subtract, Multiply, and Divide Two

l
Numbers

ita
Introduction

In Python, we can perform basic mathematical operations such as addition, subtraction,


multiplication, and division using simple arithmetic operators. This tutorial will guide you step by

ig
step on how to take two numbers as input and perform these operations.
ad
Step-by-Step Theory
Step 1: Understanding Arithmetic Operators
ith

Python provides arithmetic operators for performing calculations:

●​ + → Addition
●​ - → Subtraction
●​ * → Multiplication
an

●​ / → Division

Step 2: Taking User Input

●​ In Python, we use the input() function to take user input.


●​ Since the input is taken as a string, we must convert it into a number using float() or
int().
●​ We use float() to handle decimal numbers.

Step 3: Performing Operations


●​ We apply arithmetic operations on the two numbers.
●​ Store results in variables for clarity.

Step 4: Displaying the Results

●​ Use the print() function to show the results to the user.

l
ita
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

addition = num1 + num2


subtraction = num1 - num2
multiplication = num1 * num2

ig
division = num1 / num2 # Handles decimal division

print("\nResults:")
ad
print(f"Addition: {num1} + {num2} = {addition}")
print(f"Subtraction: {num1} - {num2} = {subtraction}")
print(f"Multiplication: {num1} × {num2} = {multiplication}")
print(f"Division: {num1} ÷ {num2} = {division}")
ith

Explanation of the Code


1.​ Taking Input
○​ input("Enter the first number: ") asks the user to enter a number.
an

○​ float() converts the input string into a number.


2.​ Performing Calculations
○​ We store the results of each operation in separate variables:
■​ addition = num1 + num2
■​ subtraction = num1 - num2
■​ multiplication = num1 * num2
■​ division = num1 / num2
3.​ Displaying Results
○​ print(f"Addition: {num1} + {num2} = {addition}") prints the
formatted result.
Example Output

l
ita
Input:

Enter the first number: 10


Enter the second number: 5

Output:

Results: ig
ad
Addition: 10.0 + 5.0 = 15.0
Subtraction: 10.0 - 5.0 = 5.0
Multiplication: 10.0 × 5.0 = 50.0
Division: 10.0 ÷ 5.0 = 2.0
ith
an
l
ita
ig
ad
ith
an

Key Takeaways

✅ input() is used to get user input.​


✅ float() converts input to a number.​
✅ Arithmetic operators (+, -, *, /) perform calculations.​
✅ print(f"...") helps display formatted output.
2.Even or Odd: Python Program to Check
if a Number is Even or Odd

l
ita
Introduction
In Python, we can determine if a number is even or odd using the modulus operator (%). This

ig
tutorial explains how to take a number as input and check whether it is even or odd.
ad
Step-by-Step Theory
Step 1: Understanding Even and Odd Numbers

●​ Even numbers are completely divisible by 2 (e.g., 2, 4, 6, 8, 10).


ith

●​ Odd numbers are not completely divisible by 2 (e.g., 1, 3, 5, 7, 9).


●​ In programming, we check if a number is divisible by 2 using the modulus (%) operator.

Step 2: Using the Modulus Operator (%)


an

●​ number % 2 == 0 → The number is even.


●​ number % 2 != 0 → The number is odd.

Step 3: Taking User Input

●​ We use the input() function to get a number from the user.


●​ Convert the input to an integer using int().

Step 4: Applying Conditional Statement

●​ Use an if statement to check if the number is even or odd.


●​ Display the result using the print() function.

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

ita
if num % 2 == 0:

print(f"{num} is an Even number.")

else:

print(f"{num} is an Odd number.")


ig
ad
Explanation of the Code
ith

1.​ Taking Input


○​ input("Enter a number: ") prompts the user to enter a number.
○​ int() converts the input into an integer.
2.​ Checking Even or Odd
○​ num % 2 == 0: If the remainder when divided by 2 is 0, it is even.
an

○​ num % 2 != 0: If the remainder is not 0, it is odd.


3.​ Displaying the Result
○​ print(f"{num} is an Even number.") prints the result.
Example Outputs

l
ita
Key Takeaways
ig
ad
✅ The modulus operator (%) helps check divisibility.​
✅ Even numbers have num % 2 == 0.​
✅ Odd numbers have num % 2 != 0.​
✅ if-else conditions help make decisions in Python.
ith
an

3.Leap Year: Python Program to Check if a


Year is a Leap Year
Introduction
A leap year is a year that has 366 days instead of 365 because February has 29 days. Leap
years occur every 4 years, but there are some exceptions.

l
Step-by-Step Theory

ita
Step 1: Understanding Leap Year Rules

A year is a leap year if:

1.​ It is divisible by 4 → year % 4 == 0

ig
2.​ Except when it is also divisible by 100 → year % 100 == 0 (not a leap year)
3.​ Unless it is also divisible by 400 → year % 400 == 0 (leap year)
ad
Step 2: Taking User Input

●​ Use input() to get the year from the user.


●​ Convert it into an integer using int().
ith

Step 3: Applying Conditional Statements

●​ Check divisibility using if-elif-else.


an

year = int(input("Enter a year: "))

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):


print(f"{year} is a Leap Year.")
else:
print(f"{year} is NOT a Leap Year.")

Explanation of the Code


1.​ Taking Input
○​ input("Enter a year: ") prompts the user to enter a year.
○​ int() converts the input to an integer.
2.​ Checking Leap Year Conditions
○​ year % 4 == 0: The year is divisible by 4.
○​ year % 100 != 0: The year should not be divisible by 100 unless...

l
○​ year % 400 == 0: If divisible by 400, it is a leap year.

ita
3.​ Displaying the Result
○​ print(f"{year} is a Leap Year.") prints the result.

Example Outputs ig
ad
ith
an

Key Takeaways
✅ A leap year must be divisible by 4.​
✅ It must not be divisible by 100, unless also divisible by 400.​
✅ The modulus operator (%) helps check divisibility.​
✅ if-elif-else statements help in decision-making.
4.Factorial: Python Program to Find the
Factorial of a Number

l
ita
Introduction
The factorial of a number (n!) is the product of all positive integers from 1 to n.

●​ Example:
○​ 5! = 5 × 4 × 3 × 2 × 1 = 120
○​ 4! = 4 × 3 × 2 × 1 = 24 ig
ad
○​ 1! = 1
○​ 0! = 1 (By definition)
ith

Step-by-Step Theory
Step 1: Understanding Factorial Calculation

The factorial of a number n is calculated as:


an

n!=n×(n−1)×(n−2)×...×1n! = n × (n-1) × (n-2) × ... × 1n!=n×(n−1)×(n−2)×...×1

For example:

●​ 3! = 3 × 2 × 1 = 6
●​ 6! = 6 × 5 × 4 × 3 × 2 × 1 = 720

Step 2: Taking User Input

●​ Use input() to get a number from the user.


●​ Convert it into an integer using int().

Step 3: Using a Loop to Compute Factorial

●​ Initialize a variable factorial = 1.

l
●​ Use a for loop to multiply numbers from 1 to n.

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

factorial = 1

if num < 0:
ig
print("Factorial is not defined for negative numbers.")
ad
elif num == 0 or num == 1:
print(f"The factorial of {num} is 1")
else:
for i in range(1, num + 1):
factorial *= i
print(f"The factorial of {num} is {factorial}")
ith

Explanation of the Code


1.​ Taking Input
an

○​ input("Enter a number: ") prompts the user to enter a number.


○​ int() converts it into an integer.
2.​ Handling Special Cases
○​ If num < 0: Factorial is not defined for negative numbers.
○​ If num == 0 or num == 1: The factorial is 1 by definition.
3.​ Using a Loop to Calculate Factorial
○​ Start with factorial = 1.
○​ Use a for loop from 1 to num, multiplying each value.
Example Outputs

l
ita
ig
ad
ith

Key Takeaways
✅ Factorial (n!) is the product of all numbers from 1 to n.​
an

✅ 0! = 1 by definition.​
✅ Factorial is not defined for negative numbers.​
✅ Loop and recursion can be used to compute factorial.​
✅ Using a loop is more memory-efficient than recursion.
5.Palindrome: Python Program to Check if
a String is a Palindrome

l
Introduction

ita
A palindrome is a word, phrase, number, or sequence of characters that reads the same
forward and backward.

Examples of Palindromes

✅ "madam" → Palindrome​
✅ "racecar" → Palindrome​
✅ "level" → Palindrome​
❌ "hello" → Not a palindrome​
ig
ad
❌ "Python" → Not a palindrome
ith

Step-by-Step Theory
Step 1: Understanding String Reversal
an

To check if a string is a palindrome:

1.​ Reverse the string.


2.​ Compare it with the original string.
3.​ If both are the same → It’s a palindrome.
4.​ If not → It’s not a palindrome.

Step 2: Taking User Input

●​ Use input() to get a string from the user.

Step 3: Ignoring Case Sensitivity


●​ Convert the string to lowercase using .lower().

Step 4: Reversing the String

●​ Use slicing: string[::-1] to reverse the string.

l
ita
Step 5: Comparing the Strings

●​ If original_string == reversed_string, it's a palindrome.

ig
text = input("Enter a string: ").lower() # Convert to lowercase

reversed_text = text[::-1]
ad
if text == reversed_text:

print(f'"{text}" is a Palindrome.')

else:
ith

print(f'"{text}" is NOT a Palindrome.')

Explanation of the Code


1.​ Taking Input
an

○​ input("Enter a string: ") gets the string from the user.


○​ .lower() converts it to lowercase for case-insensitive comparison.
2.​ Reversing the String
○​ text[::-1] creates a reversed version of the string.
3.​ Comparing the Strings
○​ If original_string == reversed_string, it is a palindrome.
○​ Otherwise, it is not a palindrome.

Example Outputs
l
ita
Key Takeaways
ig
✅ A palindrome reads the same forward and backward.​
ad
✅ Use string[::-1] for quick string reversal.​
✅ Convert to lowercase to ignore case sensitivity.​
✅ A loop-based method can also be used for checking.
ith
an
an
ith
ad
ig
ita
l

You might also like