Notes Introduction to Python
1. Programming Language
A programming language is a set of rules and instructions used to create software applications. It
allows humans to communicate with computers.
Programming languages are mainly classified into:
• Low-Level Languages: Close to machine language (e.g., Assembly, Machine Code).
• High-Level Languages: Easier to understand and use (e.g., Python, Java, C++).
Python is a high-level, interpreted, and general-purpose programming language.
2. What is Python?
Python is an easy-to-learn, interpreted, object-oriented, and high-level programming language.
Key Features of Python:
✔ Simple Syntax: Easy to read and write.
✔ Dynamically Typed: No need to declare variable types explicitly.
✔ Interpreted Language: Executes line by line without compiling.
✔ Cross-Platform: Works on Windows, Mac, and Linux.
✔ Extensive Libraries: Many built-in modules (e.g., NumPy, Pandas, Tkinter).
✔ Object-Oriented: Supports OOP concepts like classes and objects.
3. Applications of Python
Python is used in a wide range of fields, including:
1. Web Development: Frameworks like Django and Flask help build websites.
2. Data Science & Machine Learning: Libraries like NumPy, Pandas, and TensorFlow.
3. Automation & Scripting: Automates repetitive tasks (e.g., renaming files, web scraping).
4. Game Development: Pygame is used to create games.
5. Cybersecurity: Used for penetration testing and security analysis.
6. Internet of Things (IoT): Works with Raspberry Pi and microcontrollers.
4. Features of Python
Python offers several advantages:
1. Simple and Readable Syntax: Python’s syntax is similar to English.
2. Platform Independence: Runs on Windows, MacOS, Linux, etc.
3. Interpreted Language: Code is executed line by line.
4. Object-Oriented Programming (OOP): Supports concepts like classes and objects.
5. Extensive Libraries: Includes built-in libraries for various applications.
6. Free and Open Source: Available for everyone to use and modify.
7. Large Community Support: A huge developer community worldwide.
5. IDLE for Python
IDLE (Integrated Development and Learning Environment) is Python's default GUI-based
editor.
Features of IDLE:
• Provides an interactive Python Shell.
• Has a built-in text editor for writing scripts.
• Supports debugging tools.
• Allows execution of Python programs.
6. Steps to Download and Install Python IDLE
1. Go to the official Python website: [Link]
2. Click on Downloads and select your OS (Windows, macOS, or Linux).
3. Download the installer file and run it.
4. Ensure the "Add Python to PATH" checkbox is selected.
5. Click Install Now and wait for the installation to complete.
6. Open IDLE (Python’s built-in editor) to start coding.
7. Using the Python Shell in IDLE
The Python Shell in IDLE allows direct execution of Python commands.
Example:
python
print("Hello, Python!")
Output:
Hello, Python!
8. Creating and Executing a Python Program
Steps to Create a Python Program in IDLE:
1. Open IDLE.
2. Click on File > New File.
3. Type the Python code.
4. Save the file with a .py extension.
9. Steps to Save a Python Program
1. Click on File > Save As.
2. Enter a filename with .py extension (e.g., my_program.py).
3. Click Save.
10. Steps to Execute a Python Program
• Press F5 or go to Run > Run Module.
• The output will be displayed in the Python Shell.
11. Checking and Correcting Errors
Types of Errors in Python:
1. Syntax Errors: Incorrect syntax (e.g., missing colon).
python
print "Hello" # ❌ Missing parentheses
2. Runtime Errors: Errors during execution (e.g., division by zero).
3. Logical Errors: Wrong logic, no syntax error, but incorrect output.
12. The print() Command
Used to display output on the screen.
Example:
python
print("Welcome to Python!")
Output:
css
Welcome to Python!
13. Variables in Python
A variable is a name that stores data.
Example:
python
name = "Alice"
age = 25
14. Rules for Creating Variables
✔ Must start with a letter or an underscore (_).
✔ Cannot start with a number.
✔ Cannot use special characters (@, $, %).
✔ Case-sensitive (Age and age are different).
15. Data Types in Python
1. Numeric Data Types
• Integer (int): Whole numbers (5, -10).
• Floating Point (float): Decimal numbers (3.14, -0.99).
• Boolean (bool): True or False.
2. String Data Type
• A sequence of characters enclosed in quotes.
python
text = "Python is fun!"
16. Assigning Values to Variables
Example:
python
age = 30
price = 9.99
is_active = True
17. Adding Comments
• Single-line comment: # This is a comment
• Multi-line comment:
python
'''
This is a multi-line comment
'''
18. Performing Calculations in Python
Python supports arithmetic operations:
✔ + Addition
✔ - Subtraction
✔ * Multiplication
✔ / Division
✔ % Modulus
Example:
python
x = 10
y = 5
print(x + y) # Output: 15
19. Accepting Values from a User
The input() function is used to accept user input.
Example:
python
name = input("Enter your name: ")
print("Hello, " + name)
20. Formatting and Modifying Strings
Joining Two or More Strings
python
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print(full_name)
String Formatting Using format() Method
python
name = "Alice"
age = 20
print("My name is {} and I am {} years old.".format(name, age))
21. String Functions
✔ len(str): Returns length of string.
✔ upper(): Converts to uppercase.
✔ lower(): Converts to lowercase.
✔ replace(old, new): Replaces parts of a string.
Example:
python
text = "hello world"
print([Link]()) # Output: HELLO WORLD
Strings in Python
A string in Python is a sequence of characters enclosed within single ('), double ("), or triple
(''' or """) quotes. Strings are used to store and manipulate text.
Example of Strings:
# Using single quotes
name1 = 'Alice'
# Using double quotes
name2 = "Bob"
# Using triple quotes (for multi-line strings)
paragraph = '''This is a
multi-line string in Python.'''
String Operations in Python
1. Accessing Characters in a String
Strings are indexed, meaning each character has a position starting from 0.
text = "Python"
print(text[0]) # Output: P
print(text[3]) # Output: h
print(text[-1]) # Output: n (negative index starts from the end)
2. Slicing a String
We can extract a part of a string using slicing.
text = "Python Programming"
print(text[0:6]) # Output: Python
print(text[:6]) # Output: Python (same as text[0:6])
print(text[7:]) # Output: Programming
print(text[-11:-1]) # Output: Programmin
Common String Functions in Python
1. len() - Finding String Length
text = "Python"
print(len(text)) # Output: 6
2. upper() and lower() - Changing Case
text = "Hello"
print([Link]()) # Output: HELLO
print([Link]()) # Output: hello
3. strip() - Removing Spaces
text = " Hello "
print([Link]()) # Output: "Hello" (removes extra spaces)
4. replace() - Replacing Characters
text = "Hello World"
print([Link]("World", "Python")) # Output: Hello Python
5. split() - Splitting a String into a List
text = "Python,Java,C++"
words = [Link](",")
print(words) # Output: ['Python', 'Java', 'C++']
6. join() - Joining Strings from a List
words = ["Python", "Java", "C++"]
result = "-".join(words)
print(result) # Output: Python-Java-C++
String Formatting in Python
1. Using format() Method
name = "Alice"
age = 25
print("My name is {} and I am {} years old.".format(name, age))
2. Using f-strings (Python 3.6+)
name = "Bob"
age = 30
print(f"My name is {name} and I am {age} years old.")
Escape Sequences in Strings
Escape sequences allow inserting special characters in strings.
Escape Sequence Meaning
\n Newline
\t Tab Space
\' Single Quote
\" Double Quote
\\ Backslash
Example:
print("Hello\nWorld") # Output: Hello (new line) World
print("He said, \"Python is fun!\"") # Output: He said, "Python is fun!"