Python
• Python is a high-level, interpreted, and general-
purpose programming language that is easy to
read, write, and understand. It is widely used for
web development, data analysis, artificial
intelligence, automation, and many other
applications.
Real-Life Use-cases of python:
Here are some real-life use cases of Python
• Web Development
• Used to create websites and web apps.
• Example: Instagram, Spotify, and YouTube use Python in their backend.
• Data Analysis & Visualization
• Helps analyze large amounts of data and make reports/graphs.
• Example: Companies like Netflix use it to analyze user behavior.
• Artificial Intelligence & Machine Learning
• Used to build smart systems like recommendation engines and chatbots.
• Example: Amazon Alexa, Google Assistant, or Netflix recommendations.
• Automation (Scripting)
• Automates repetitive tasks (like renaming files, sending emails, or scraping
websites).
• Example: A company can automatically send invoices to clients.
• Game Development
• Python is used in making simple and even professional games.
• Example: Games like Civilization IV used Python for scripting.
• Scientific & Research Applications
• Used in science labs and research organizations for simulations, statistics, and experiments.
• Example: NASA uses Python for space-related research.
• Finance & Business
• Used for stock market analysis, risk management, and fintech apps.
• Example: Banks and financial companies use Python to detect fraud.
• In short, Python is everywhere — from your mobile apps and movies on Netflix to AI assistants
like Siri and Alexa.
History of Python
1980s (Late):
• Guido van Rossum, a Dutch programmer, started
developing Python while working at CWI (Centrum
Wiskunde & Informatica), Netherlands.
• He wanted a language that was easy to read and
powerful like ABC language but also allowed
handling system tasks like C.
1991 (Python 0.9.0 Released):
• First public release by Guido van Rossum.
• Included features like: functions, exception handling,
and the core data types (str, list, dict).
1994 (Python 1.0):
• Official stable version released.
• Introduced modules, exception handling, functions, and basic built-in types.
2000 (Python 2.0):
• Released by Python Software Foundation (PSF).
• Added list comprehensions and garbage collection.
• Became very popular, but had backward compatibility issues.
2008 (Python 3.0):
• Released to fix design flaws of Python 2.
• Improved Unicode support, print() as a function, better libraries.
• Python 2 and Python 3 were incompatible, so both coexisted for years.
2020 (End of Python 2):
• Python 2 officially retired on January 1, 2020.
• Now, Python 3.x is the only active version, widely used across industries.
How Python is Installed
• Python is free and open-source.
• Can be downloaded from [Link].
• Supports Windows, Linux, and macOS.
Installation includes:
• Python Interpreter
• IDLE (Integrated Development & Learning Environment)
• Standard Libraries
• Important: Check "Add Python to PATH" option during install.
• Alternative: Anaconda Distribution (popular in Data Science)
• Check installation:
python --version
Execution of the Basic Program
• Interactive Mode (REPL):
Type commands directly.
• Example:
>>> print("Hello, Python!")
• Script Mode (.py file):
• Write program in a text editor.
• Save as [Link].
• Run with:
>>>python [Link]
Character Set in Python
• The character set is the collection of valid characters that can be
used in a programming language.
• Python programs are written using characters such as letters,
digits, symbols, and spaces.
• Python uses Unicode, so it supports characters from almost every
written language in the world (including Hindi, Chinese, emojis,
etc.).
Types of Characters in Python
• Letters:
• Uppercase → A–Z
• Lowercase → a–z
• Digits:
• 0–9
• Special Symbols:
• + - * / % = < > ( ) { } [ ] @ , . : etc.
• Whitespace Characters:
• Space, Tab, Newline → used for indentation (very important
in Python).
• Other Characters (Unicode):
• Supports characters from all languages and even emojis.
# Using letters and digits
• name = "Python3"
# Using special symbols
• result = 10 + 5
# Using whitespace (indentation is mandatory in Python)
• if result > 10:
• print("Result is greater than 10")
# Using Unicode characters
• print("नमस्ते") # Hindi
• print("你好") # Chinese
• print(" ") # Emoji
Tokens in Python
• Tokens are the smallest building blocks of a Python program.
• A Python program is made up of different tokens like keywords, identifiers, literals, operators, and punctuators.
Types of Tokens
• Keywords
• Predefined reserved words.
• Cannot be used as variable names.
• Example: if, else, while, for, def, True, False, None.
• Identifiers
• Names given by the user for variables, functions, classes, etc.
• Example: age, student_name, myFunction.
• Literals
• Fixed values in a program.
• Types: numbers (10, 3.14), strings ("Hello"), boolean (True/False).
• Operators
• Symbols that perform operations.
• Example: + - * / % ==.
• Punctuators (Separators)
• Symbols that structure the code.
• Example: : ( ) { } [ ] , ..
# Keywords
• if True:
• print("This is a keyword example")
# Identifiers
• student_name = "Alice"
# Literals
• marks = 95 # integer literal
• pi = 3.14 # float literal
• greeting = "Hello" # string literal
# Operators
• total = marks + 5
# Punctuators
• for i in range(3):
• print(i)
Core Data Types in Python
• What are Data Types?
• Data type defines the kind of data a variable can hold.
• Python has built-in core data types that are used most frequently.
• Since Python is dynamically typed, you don’t need to declare the type explicitly — it’s assigned
automatically.
Core Data Types
• Numbers
• int → whole numbers (10, -5)
• float → decimal numbers (3.14, -2.5)
• complex → numbers with real + imaginary parts (3 + 2j)
• String (str)
• Sequence of characters enclosed in quotes.
• "Hello", 'Python'
Boolean (bool)
• Logical
values: True or False.
List
• Ordered, mutable
collection.
• [1, 2, 3, "Hi"]
Tuple
• Ordered, immutable
collection.
• (1, 2, 3)
Dictionary (dict)
• Key-value pairs.
• {"name": "Alice", "age": 25}
# Numbers
• a = 10 # int
• b = 3.14 # float
• c = 2 + 3j # complex
# String
• name = "Python"
# Boolean
• is_active = True
# List
• fruits = ["apple", "banana", "cherry"]
# Tuple
• colors = ("red", "green", "blue")
# Dictionary
• student = {"name": "Alice", "marks": 90}
What is a Variable?
• A variable is a name given to a memory location that stores
data.
• In Python, variables are created automatically when you
assign a value (no need for explicit declaration).
• Python is dynamically typed, meaning the type of a
variable is decided at runtime.
Variables in
Python Rules for Naming Variables
• Must begin with a letter (A–Z / a–z) or an underscore (_).
• Cannot start with a digit.
• Can only contain letters, digits, and underscores.
• Case-sensitive → Name ≠ name.
• Cannot use keywords as variable names (if, class, while).
# Valid variables
• x = 10
• name = "Python"
• _pi = 3.14
# Case sensitivity
• age = 20
• Age = 30 # different from 'age'
# Invalid examples
• 2value = 100 # cannot start with digit
• if = 50 # cannot use keyword
• 1. input() Function
• Used to take input from the user.
input(), • Always returns data as a string by default.
eval(), & • Syntax:
variable = input("Message for user")
print() in Example:
Python name = input("Enter your name: ")
print("Hello,", name)
• 2. eval() Function
• Used to evaluate expressions entered by the user.
• Converts input into the correct data type (int, float, etc.).
• Syntax:
variable = eval(input("Enter expression: "))
Example:
x = eval(input("Enter math expression: ")) # e.g., 5+3
print("Result:", x)
• 3. print() Function
• Used to display output on the screen.
• Can print strings, numbers, variables, or multiple values.
• Syntax:
print(value1, value2, ..., sep=" ", end="\n")
Example:
print("Python", 3, sep="-") # Output: Python-3
print("Hello", end=" ")
print("World") # Output: Hello World
What is String Formatting?
• String formatting helps in inserting variables/values into strings.
• Makes output more readable and dynamic.
Ways to Format Strings
• Concatenation using +
name = "John"
Formatting print("Hello " + name)
• Using format() method
Strings in age = 25
Python
print("My name is {} and I am {} years old".format("John", age))
• f-strings (Python 3.6+) → Most Preferred
name = "John"
age = 25
print(f"My name is {name} and I am {age} years old")
• What are Operators?
Operators are symbols that perform operations on
values/variables.
An expression combines variables, constants, and
operators to produce a result.
• Types of Operators
Operators 1. Arithmetic Operators → + - * / % // **
and • print(5+2, 5//2, 5**2) # 7 2 25
2. Relational (Comparison) Operators → == != > < >= <=
Expressions. print(5 > 3, 5 == 2) # True False
3. Logical Operators → and or not
print(True and False) # False
print(5 > 3 or 2 > 10) # True
Assignment Operators → = += -= *= /=
• x = 10
• x += 5 # 15
• x *= 2 # 30
Bitwise Operators → & | ^ << >>
print(5 & 3) # 1
print(5 | 3) # 7
Expressions in Python
• An expression is a combination of variables, constants, and operators that
produces a result.
Example:
a=5
b=3
result = a + b * 2 # 11