1.
Python Basics
1.1 Python:
Python is a popular programming language known for its simplicity
and readability. It is used for creating applications, websites, and
handling data.
1.2 Syntax:
The set of rules that defines how a Python program is written and
interpreted.
2. Variables and Data Types
2.1 Variables:
A variable is a name that stores a value. For example, x = 10 stores
the value 10 in the variable x.
2.2 Data Types:
These define the kind of data a variable can hold:
• int: Numbers (e.g., 10)
• float: Decimal numbers (e.g., 10.5)
• str: Text or strings (e.g., "Hello")
• bool: True or False values
• list: A collection of items (e.g., [1, 2, 3])
• tuple: Like a list but cannot be changed (e.g., (1, 2, 3))
• dict: A collection of key-value pairs (e.g., {"name": "John",
"age": 25})
3. Input and Output
3.1 Input:
Takes data from the user using the input() function.
Example:
python
CopyEdit
name = input("What is your name? ")
3.2 Output:
Displays data to the user using the print() function.
Example:
python
CopyEdit
print("Hello, world!")
4. Operators
• Arithmetic Operators: Used for mathematical operations (+, -,
*, /, etc.).
• Comparison Operators: Used to compare values (==, !=, >, <,
etc.).
• Logical Operators: Used to combine conditions (and, or, not).
5. Conditional Statements
These are used to make decisions in a program.
• if: Executes a block of code if a condition is true.
Example:
python
CopyEdit
if x > 0:
print("Positive number")
• else: Executes a block of code if the condition is false.
• elif: Checks multiple conditions.
6. Loops
Loops are used to repeat actions.
• for loop: Repeats a block of code for a specific number of times.
Example:
python
CopyEdit
for i in range(5):
print(i)
• while loop: Repeats a block of code as long as the condition is
true.
Example:
python
CopyEdit
while x > 0:
print(x)
x -= 1
7. Functions
A function is a block of reusable code that performs a specific task.
Example:
python
CopyEdit
def greet(name):
print("Hello, " + name)
greet("Alice")
8. Lists
A list is a collection of items in a particular order.
Example:
python
CopyEdit
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Outputs "apple"
9. Strings
A string is a sequence of characters. Strings are enclosed in quotes.
Example:
python
CopyEdit
greeting = "Hello"
print(greeting.upper()) # Outputs "HELLO"
10. Error Handling
Errors in Python can be handled using try-except blocks.
Example:
python
CopyEdit
try:
x = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")
1. Hello, World!
Code:
print("Hello, World!")
Output:
Hello, World!
2. Add Two Numbers
Code:
a=5
b=3
sum = a + b
print("The sum is:", sum)
Output:
The sum is: 8
3. Check Even or Odd
Code:
num = 7
if num % 2 == 0:
print(num, "is Even")
else:
print(num, "is Odd")
Output:
7 is Odd
4. Find the Largest Number
Code:
a, b, c = 10, 25, 15
largest = max(a, b, c)
print("The largest number is:", largest)
Output:
The largest number is: 25
5. Simple Interest
Code:
P = 1000 # Principal amount
R=5 # Rate of interest
T=2 # Time in years
SI = (P * R * T) / 100
print("The Simple Interest is:", SI)
Output:
The Simple Interest is: 100.0
6. Factorial of a Number
Code:
num = 5
factorial = 1
for i in range(1, num + 1):
factorial *= i
print("Factorial of", num, "is", factorial)
Output:
Factorial of 5 is 120
7. Check if a Number is Prime
Code:
num = 11
is_prime = True
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
is_prime = False
break
if is_prime:
print(num, "is a Prime Number")
else:
print(num, "is not a Prime Number")
Output:
11 is a Prime Number
8. Print Multiplication Table
Code:
num = 7
for i in range(1, 11):
print(num, "x", i, "=", num * i)
Output:
7x1=7
7 x 2 = 14
7 x 3 = 21
...
7 x 10 = 70
9. Reverse a String
Code:
string = "Python"
reversed_string = string[::-1]
print("Reversed string is:", reversed_string)
Output:
Reversed string is: nohtyP
10. Check Palindrome
Code:
string = "madam"
if string == string[::-1]:
print(string, "is a Palindrome")
else:
print(string, "is not a Palindrome")
Output:
madam is a Palindrome