Python Uint 1
Python Uint 1
🔹 Introduction to Python
✅ What is Python?
Feature Description
Simple Syntax Python uses English-like commands, making it beginner-friendly.
Interpreted No need to compile code. Python runs line-by-line.
Cross-Platform Works on Windows, macOS, and Linux.
Dynamically No need to declare variable types; Python figures it out during
Typed execution.
Open Source Free to use and distribute, with a large community for support.
Versatile Can be used for web development, AI, data science, automation, etc.
Python is interpreted, meaning the code you write is read and executed line by line by the
Python interpreter. This is different from compiled languages like C++, where code must be
compiled before running.
✅ Installing Python:
1. Download Python:
o Go to https://www.python.org
o Download the latest version (preferably Python 3.x).
2. Install Python:
o Run the downloaded installer.
o Important: Check the box that says “Add Python to PATH” during
installation.
o Click “Install Now”.
3. Verify Installation:
o Open a terminal (Command Prompt on Windows or Terminal on Mac/Linux).
o Type:
o python --version
or
python3 --version
Example program:
print("Hello, World!")
To run:
Save it as hello.py
Run in terminal:
python hello.py
📝 Practice Task:
1. Install Python.
2. Open any editor or IDLE.
3. Write your first program:
4. print("Welcome to Python Programming!")
5. Run it and see the output.
🔹 Variables and Data Types in Python
✅ What is a Variable?
A variable is a name that refers to a value stored in memory. You use variables to store data
and use it later in the program.
➤ Declaring a variable:
Python does not require specifying the data type when declaring a variable.
x = 10 # Integer
name = "John" # String
pi = 3.14 # Float
Python determines the type automatically based on the assigned value. This is known as
dynamic typing.
Examples:
_valid = 1
user_name = "Alice"
age2 = 20
🔸 Data Types in Python
Python has several built-in data types. Let’s go over the most important ones:
✅ 1. Numeric Types
✅ 2. Text Type
✅ 3. Boolean Type
✅ 4. Sequence Types
a=5
print(type(a)) # <class 'int'>
b = "Python"
print(type(b)) # <class 'str'>
📝 Practice Task:
x = 42
name = "Python"
marks = 85.5
is_passed = True
colors = ["red", "green", "blue"]
person = {"name": "Alice", "age": 22}
print(type(x))
print(type(name))
print(type(marks))
print(type(is_passed))
print(type(colors))
print(type(person))
🔹 Operators in Python
Operators are symbols that perform operations on variables and values. Python supports
several types of operators.
✅ 1. Arithmetic Operators
+ Addition 5+3 8
- Subtraction 5-3 2
* Multiplication 5*3 15
// Floor Division 5 // 2 2
% Modulus (remainder) 5 % 2 1
** Exponentiation 2 ** 3 8
🔸 Example:
a = 10
b=3
print(a + b)
print(a % b)
print(a ** b)
✅ 2. Assignment Operators
= x=5 Assign 5 to x
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 2 x=x*2
Operator Example Same As
/= x /= 2 x=x/2
//= x //= 2 x = x // 2
%= x %= 2 x=x%2
**= x **= 2 x = x ** 2
✅ 3. Comparison Operators
== Equal to 5 == 5 → True
✅ 4. Logical Operators
✅ 6. Identity Operators
🔸 Example:
a = [1, 2]
b=a
c = [1, 2]
print(a is b) # True
print(a is c) # False (different memory location)
` ` OR
^ XOR 5^3→6
~ NOT (invert) ~5
a = 10
b=3
print("Addition:", a + b)
print("Is a greater than b?", a > b)
print("Bitwise AND:", a & b)
print("a is b:", a is b)
print("a in [10, 20]:", a in [10, 20])
These are sequence data types in Python used to store collections of items. Each has
different properties in terms of mutability and usage.
✅ 1. List Operations
➤ Creating a List:
✅ 2. Tuple Operations
➤ Creating a Tuple:
🔸 Tuples are faster and safer than lists when you don’t need to modify the data.
✅ 3. String Operations
➤ Creating a String:
📝 Practice Task:
# List operations
numbers = [10, 20, 30, 40]
numbers.append(50)
print(numbers)
numbers.remove(20)
print(numbers[1:3])
# Tuple operations
info = ("Alice", 21, "Student")
print(info[0])
print(len(info))
# String operations
text = " Python Programming "
print(text.strip())
print(text.upper())
print(text.replace("Python", "Java"))
This topic focuses on combining data types, operators, and logical thinking to solve real-
world problems using Python.
You can calculate everything from shopping bills to scientific values using Python's
arithmetic operators.
radius = 5
pi = 3.14
area = pi * (radius ** 2)
print("Area of circle is:", area)
math = 85
science = 90
english = 80
print("Total:", total)
print("Percentage:", percentage)
These are used in if, else, and elif conditions to control program flow.
price = 1200
member = True
marks = 75
print("Grade:", grade)
if username in users:
print("Access granted")
else:
print("Access denied")
📌 Example 7: is vs ==
a = [1, 2]
b = [1, 2]
c=a
✅ Practice Problems