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

Python Class8

Python 8

Uploaded by

Himanshu Kumar
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)
2 views15 pages

Python Class8

Python 8

Uploaded by

Himanshu Kumar
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

Class-8

Introduction to Python

Python is a high-level, interpreted


programming language.
It is simple, powerful, and widely used.
Applications of Python:

 Web Development
 Artificial Intelligence (AI)
 Data Science

 Game Development
 Automation
Features of Python

 Easy to learn and use


 Open-source and free
 Platform-independent
 Supports Object-Oriented Programming
(OOP)
---
Input and Output
Output using print()
print("Welcome to Python")
Multiple Outputs
name = "Aman"
age = 13
print("Name:", name)
print("Age:", age)
Input from User
name = input("Enter your name: ")
print("Hello", name)
Numeric Input
num = int(input("Enter a number: "))
print("You entered:", num)
---
3: Variables and Data Types
Variables
Variables are used to store data.
x = 10
name = "Aman"
Data Types
Data Type Example
Integer (int) 25
Float 3.14
String (str) "Python"
Boolean (bool) True, False
List[1, 2, 3]
---
4: Operators
Arithmetic Operators
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
// Floor Division
** Exponent
Example:
a = 10
b=3
print(a + b)
print(a // b)
print(a ** b)
Comparison Operators
Operator Meaning
== Equal
!= Not Equal
> Greater Than
< Less Than
>= Greater Than or Equal
<= Less Than or Equal
---
5: Conditional Statements
if Statement
num = 10
if num > 0:
print("Positive Number")
if-else Statement
num = 5
if num % 2 == 0:
print("Even")
else:
print("Odd")
if-elif-else Statement
marks = 85
if marks >= 90:
print("A+")
elif marks >= 75:
print("A")
elif marks >= 60:
print("B")
else:
print("C")
---
6: Loops
for Loop
for i in range(1, 6):
print(i)
while Loop
count = 1
while count <= 5:
print(count)
count += 1
Nested Loop
for i in range(1, 4):
for j in range(1, 4):
print(i, j)
---
7: Strings
Creating Strings
name = "Python"
String Functions
text = "python programming"
print(len(text))
print([Link]())
print([Link]())
String Slicing
word = "COMPUTER"
print(word[0:4])
print(word[-3:])
---
8: Lists
Creating a List
numbers = [10, 20, 30, 40]
Accessing Elements
print(numbers[0])
List Methods
[Link](50)
[Link](20)
print(numbers)
Looping Through a List
for item in numbers:
print(item)
---
9: Tuples and Dictionaries
Tuple
colors = ("Red", "Green", "Blue")
print(colors[1])
Dictionary
student = {
"name": "Aman",
"class": 8,
"marks": 85
}
print(student["name"])
---
10: Functions
Simple Function
def greet():
print("Welcome")
greet()
Function with Parameters
def add(a, b):
return a + b
result = add(5, 7)
print(result)
---
11 Modules
Modules are files containing Python code that
can be reused.
Example using math module:
import math
print([Link](25))
Output:
5.0
---
12: Exception Handling
Exception handling prevents program crashes.
try:
num = int(input("Enter a number: "))
print(num)
except:
print("Invalid Input")
---
Chapter 13: Simple Programs
Prime Number Check
num = 7
count = 0
for i in range(1, num + 1):
if num % i == 0:
count += 1
if count == 2:
print("Prime Number")
else:
print("Not Prime")
Factorial Program
num = 5
fact = 1
for i in range(1, num + 1):
fact *= i
print("Factorial =", fact)
Fibonacci Series
a, b = 0, 1
for i in range(10):
print(a, end=" ")
a, b = b, a + b
---
Important Points
 Python is case-sensitive.
 Proper indentation is required.
 Use comments (#) to explain code.
 Functions help reduce repeated code.
 Lists, tuples, and dictionaries are
important data structures.
---
Quick Revision
✅ Variables and Data Types
✅ Input and Output
✅ Operators
✅ Conditional Statements
✅ Loops and Nested Loops
✅ Strings and String Functions
✅ Lists, Tuples, Dictionaries
✅ Functions and Return Values
✅ Modules
✅ Exception Handling
✅ Prime, Factorial, and Fibonacci Programs
Viva Questions
1. What is Python?
2. What is a variable?
3. Difference between a list and a tuple?
4. What is a function?
5. What is a module?
6. What is exception handling?
7. What is the use of range()?
8. Explain string slicing with an example.

You might also like