Introduction to Python Programming (Class X)
1. What is Python?
Python is a high-level, easy-to-learn programming language used in:
- Web development
- Artificial Intelligence
- Data Science
- Game development
- Automation
Features of Python
- Simple syntax
- Easy to read and write
- Platform independent
- Large number of libraries
2. Tokens in Python
Definition
Tokens are the smallest units in a Python program.
Types of Tokens
- Keywords
- Identifiers
- Literals
- Operators
- Punctuators
Example
a = 10
Here:
a→ Identifier=→ Operator10→ Literal
3. Variables
Definition
In Python, a variable is a symbolic name that acts as a reference or pointer to an object stored in memory.
Example
name = "Rahul"
age = 15
4. Rules for Naming Variables/Identifiers
Valid Rules
- Must start with a letter or
_ - Cannot start with a number
- Cannot contain spaces and special symbols like @#$%^&*
- Cannot use keywords
Valid Examples
student_name
_marks
age1
Invalid Examples
1name
student name
class
5. Data Types in Python
| Data Type | Example |
|---|---|
Integer (int) | 10 |
Float (float) | 3.14 |
String (str) | "Hello" |
Boolean (bool) | True |
| List | [1,2,3] |
| Tuple | (1,2,3) |
| Set | {1,2,3} |
| Dictionary | {"a":1} |
6. Operators in Python
Operators are special symbols or keywords. Operators are used to perform operations on variables and values.
A. Arithmetic Operators
| Operator | Meaning | Example |
|---|---|---|
+ | Addition | 5+2 |
- | Subtraction | 5-2 |
* | Multiplication | 5*2 |
/ | Division | 5/2 |
% | Modulus | 5%2 |
// | Floor Division | 5//2 |
** | Exponent | 2**3 |
Example:
a = 10
b = 3
print(a+b)
print(a%b)
B. Assignment Operators
| Operator | Example |
|---|---|
= | a = 5 |
+= | a += 2 |
-= | a -= 2 |
*= | a *= 2 |
C. Comparison Operators
| Operator | Meaning |
|---|---|
== | Equal |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater than equal |
<= | Less than equal |
Example:
print(5 > 2)
D. Logical Operators
| Operator | Meaning |
|---|---|
and | Both conditions true |
or | Any one condition true |
not | Reverse result |
Example:
print(True and False)
E. Identity Operators
| Operator | Meaning |
|---|---|
is | Same object |
is not | Different object |
F. Membership Operators
| Operator | Meaning |
|---|---|
in | Present |
not in | Not present |
Example:
a = [1,2,3]
print(2 in a)
G. Bitwise Operators
| Operator | Meaning |
|---|---|
& | AND |
| ` | ` |
^ | XOR |
~ | NOT |
<< | Left Shift |
>> | Right Shift |
7. Comments in Python
Comments are notes written in the source code that the interpreter completely ignores during execution. They are used to explain code, make it more readable and understable.
Single-line Comment
# This is a comment
Multi-line Comment
"""
This is
multi-line comment
"""
8. Control Structures
Control structures decide the flow of execution.
Types:
- Conditional Statements
- Loops
9. Conditional Statements
A. if Statement
age = 18
if age >= 18:
print("Eligible")
B. if-else Statement
num = 5
if num % 2 == 0:
print("Even")
else:
print("Odd")
C. if-elif-else Statement
marks = 85
if marks >= 90:
print("A")
elif marks >= 75:
print("B")
else:
print("C")
D. Nested if
num = 10
if num > 0:
if num % 2 == 0:
print("Positive Even")
10. Loops in Python
Loops are used to repeat statements. In Python, loops are control structures used to repeat a block of code multiple times. There are two primary types of loops: for loops and while loops.
A. for Loop
for i in range(5):
print(i)
Output:
0
1
2
3
4
B. while Loop
i = 1
while i <= 5:
print(i)
i += 1
11. Built-in Functions
Python’s built-in functions are pre-defined tools available in the interpreter without requiring any import statements. Some important buil-in functions are print(), input(), range()
A. print()
Used to display output on the screen.
print("Hello")
B. input()
Used to take input from user.
name = input("Enter your name: ")
print(name)
C. range()
Used in loops to generate sequence.
for i in range(1,6):
print(i)
12. Lists in Python
Definition
List is an ordered, mutable collection.
fruits = ["apple", "banana", "mango"]
A. Modify Element
fruits[1] = "orange"
print(fruits)
B. Add Element
append()
fruits.append("grapes")
insert()
fruits.insert(1, "kiwi")
C. Remove Element
remove()
fruits.remove("apple")
pop()
fruits.pop()
D. Sorting List
numbers = [4,1,3,2]
numbers.sort()
print(numbers)
13. Tuple
Definition
Tuple is an ordered and immutable collection.
t = (1,2,3)
Properties of Tuple
- Ordered
- Immutable
- Allows duplicate values
Accessing Tuple Elements
print(t[0])
14. Sets in Python
Definition
Set is an unordered collection of unique elements.
s = {1,2,3}
Properties of Set
- Unordered
- No duplicate values
- Mutable
A. Adding Elements
s.add(4)
B. Removing Elements
s.remove(2)
C. Union
Combines elements of two sets.
a = {1,2,3}
b = {3,4,5}
print(a.union(b))
D. intersection_update()
Updates common elements.
a = {1,2,3}
b = {2,3,4}
a.intersection_update(b)
print(a)
E. intersection()
Returns common elements.
a = {1,2,3}
b = {2,3,4}
print(a.intersection(b))
15. Dictionary in Python
Definition
Dictionary stores data in key-value pairs.
student = {
"name":"Rahul",
"marks":90
}
Properties of Dictionary
- Stores key-value pairs
- Mutable
- Keys must be unique
A. get()
print(student.get("name"))
B. keys()
print(student.keys())
C. values()
print(student.values())
D. items()
print(student.items())
E. update()
student.update({"age":15})
F. pop()
student.pop("marks")
16. Python Libraries
Libraries provide extra functionality.
A. NumPy
NumPy is used for numerical calculations and arrays.
Example:
import numpy as np
arr = np.array([1,2,3])
print(arr)
B. Pandas
Pandas is used for data analysis.
Example:
import pandas as pd
C. OpenCV
OpenCV is used for image processing and computer vision.
Example:
import cv2
D. Matplotlib
Matplotlib is used for graphs and charts.
Example:
import matplotlib.pyplot as plt
17. Practice Questions
Very Short Answer
- What is a variable?
- Name two data types.
- What is a comment?
Short Answer
- Differentiate between list and tuple.
- Explain
if-elsestatement with example. - Write properties of set.
Programs
- Write a program to check even or odd.
- Write a program to print numbers from 1 to 10.
- Create a list and sort it.
- Create a dictionary of student details.
18. Summary
In this chapter you learned:
- Tokens and variables
- Data types
- Operators
- Conditional statements and loops
- Lists, tuples, sets, dictionaries
- Built-in functions
- Python libraries
Python is beginner-friendly and widely used in modern technology fields.