Python Note For Class 9th
Python Note For Class 9th
Python is a popular high-level programming language that was created by Guido van Rossum and
first released in 1991. It is known for its simplicity, readability, and versatility, making it a preferred
language for various applications such as web development, data analysis, machine learning, artificial
intelligence, and scripting.
Python's design philosophy emphasizes code readability, which means that its syntax and structure
are designed to be clear and intuitive, making it easier for programmers to write and understand
code. This readability is achieved through the use of indentation and a straightforward syntax,
making Python an excellent choice for beginners.
Web Development: Python provides several frameworks like Django, Flask, and Pyramid, which
simplify web development. These frameworks offer tools and libraries for handling routing,
templating, database interaction, and other web-related tasks.
Data Analysis and Visualization: Python has become a popular language for data analysis and
visualization. Libraries such as NumPy, Pandas, and Matplotlib provide powerful tools for data
manipulation, analysis, and visualization. Additionally, tools like Jupyter Notebook and pandas make
it easy to work with data and create interactive data visualizations.
Machine Learning and Artificial Intelligence: Python has gained significant popularity in the field of
machine learning and AI. Libraries such as scikit-learn, TensorFlow, Keras, and PyTorch offer a wide
range of tools and algorithms for building and training machine learning models. Python's simplicity
and rich ecosystem make it an ideal choice for researchers and practitioners in this field.
Scientific Computing: Python is widely used for scientific computing tasks due to its extensive
libraries and tools. Libraries like SciPy, NumPy, and pandas provide efficient and convenient methods
for scientific calculations, linear algebra, optimization, and more.
Scripting and Automation: Python's easy-to-understand syntax and versatility make it a powerful
scripting language. It is often used for automating repetitive tasks, writing system scripts, and
creating command-line tools.
Game Development: Python has libraries like Pygame and Panda3D that enable game development.
These libraries provide functions for handling graphics, physics, and game logic, making it easier to
develop games.
Internet of Things (IoT): Python's simplicity and support for libraries like Raspberry Pi provide an
excellent platform for IoT projects. Developers can leverage Python's capabilities to interact with
sensors, control devices, and collect data in IoT applications.
Web Scraping: Python's libraries, such as BeautifulSoup and Scrapy, make it easy to extract data from
websites. Web scraping allows you to automate the extraction of information from web pages, which
can be useful for data collection, analysis, or building web applications.
These are just a few examples of Python's applications. The language's versatility and extensive
libraries enable it to be used in various other domains, including networking, cybersecurity, finance,
natural language processing, and more. Python's popularity and active community contribute to its
continuous growth and adoption in diverse fields.
Python Basics
VARIABLES:
In Python, variables are used to store and manipulate data. They serve as containers that
hold values, and the value stored in a variable can change throughout the program
execution. Variables in Python are dynamically typed, meaning you don't need to declare the
variable's type explicitly.
Let's explore the concepts of declaration, definition, and initialization of variables in Python:
Declaration: In Python, you don't need to explicitly declare variables before using them. You
can simply assign a value to a variable, and Python will automatically create and associate
the variable with that value. For example:
x=5
In this case, x is the variable, and it is declared and associated with the value 5 without any
explicit declaration.
Definition: In Python, the definition of a variable happens at the moment when you assign a
value to it. When you assign a value to a variable, you are defining it and associating that
value with the variable name. For example:
x=5
Here, x is defined as a variable and assigned the value 5.
Initialization: Initialization refers to the process of assigning an initial value to a variable
when it is defined. In Python, you can initialize a variable at the time of declaration or later
in the program. For example:
x = 5 # Initialization at the time of declaration
y # Declaration
y = 10 # Initialization later in the program
In the above code snippet, x is initialized with the value 5 at the time of declaration. In
contrast, y is declared without an initial value and later initialized with the value 10.
It's important to note that Python allows variables to change their values dynamically during
program execution. You can assign a new value to a variable at any point in your code, even
after it has been declared and initialized.
python
Copy code
x=5
print(x) # Output: 5
x = 10
print(x) # Output: 10
In this example, the variable x is first assigned the value 5 and then reassigned the value 10.
The output of the two print statements reflects the updated value of x.
Remember that Python is dynamically typed, so you don't need to explicitly specify the type
of a variable. The type of a variable is determined by the value assigned to it, and it can
change if a different value of a different type is assigned to the same variable later in the
program.
Python operators
Python provides a wide range of operators that allow you to perform various
operations on data. Here are some commonly used operators in Python along
with examples:
Arithmetic Operators:
Addition (+): Adds two values together.
a=5+3
print(a) # Output: 8
Subtraction (-): Subtracts one value from another.
b = 10 - 4
print(b) # Output: 6
Multiplication (*): Multiplies two values.
c=2*5
print(c) # Output: 10
Division (/): Divides one value by another (result is a float).
d = 15 / 3
print(d) # Output: 5.0
Floor Division (//): Divides one value by another and rounds down to the
nearest integer.
e = 15 // 4
print(e) # Output: 3
Modulo (%): Returns the remainder after division.
f = 15 % 4
print(f) # Output: 3
Exponentiation (**): Raises a value to the power of another.
g = 2 ** 3
print(g) # Output: 8
Comparison Operators:
Equal to (==): Checks if two values are equal.
a = 5 == 5
print(a) # Output: True
Not equal to (!=): Checks if two values are not equal.
b = 5 != 3
print(b) # Output: True
Greater than (>): Checks if the left value is greater than the right value.
c=8>6
print(c) # Output: True
Less than (<): Checks if the left value is less than the right value.
d=4<7
print(d) # Output: True
Greater than or equal to (>=): Checks if the left value is greater than or equal to
the right value.
e = 8 >= 8
print(e) # Output: True
Less than or equal to (<=): Checks if the left value is less than or equal to the
right value.
f = 4 <= 6
print(f) # Output: True
Assignment Operators:
Assignment (=): Assigns a value to a variable.
a=5
Addition assignment (+=): Adds a value to the variable and assigns the result to
the variable.
b=3
b += 2 # Equivalent to b = b + 2
print(b) # Output: 5
Subtraction assignment (-=): Subtracts a value from the variable and assigns
the result to the variable.
c = 10
c -= 4 # Equivalent to c = c - 4
print(c) # Output: 6
Multiplication assignment (*=): Multiplies the variable by a value and assigns
the result to the variable.
d=2
d *= 5 # Equivalent to d = d * 5
print(d) # Output: 10
Division assignment (/=): Divides the variable by a value and assigns the result
to the variable.
e = 15
e /= 3 # Equivalent to e = e / 3
print(e) # Output: 5.0
Logical Operators:
Accessing Values:
Dictionary values can be accessed using their corresponding keys.
print(student['name']) # Output: 'Alice'
print(student['age']) # Output: 25
Modifying Values:
Dictionary values can be modified by assigning a new value to a specific key.
python
Copy code
student['age'] = 26
print(student['age']) # Output: 26
Dictionary Operations:
Adding Elements:
Assigning a new value to a new key.
Using the update() method to add multiple key-value pairs.
Removing Elements:
Using the del keyword to remove a specific key-value pair.
Using the pop() method to remove and return a value associated with a specific
key.
Other Operations:
len(): Returns the number of key-value pairs in the dictionary.
keys(): Returns a list of all keys in the dictionary.
values(): Returns a list of all values in the dictionary.
Dictionary Iteration:
# Output:
# name Alice
# age 26
# grade A
Dictionary Methods:
Python provides various built-in methods for working with dictionaries. Some
commonly used methods include:
get(): Returns the value associated with a given key, or a default value if the key
is not found.
keys(): Returns a view object of all keys in the dictionary.
values(): Returns a view object of all values in the dictionary.
items(): Returns a view object of all key-value pairs in the dictionary.
Example:
python
Copy code
print(student.get('name')) # Output: 'Alice'
print(student.keys()) # Output: dict_keys(['name', 'age', 'grade'])
print(student.values()) # Output: dict_values(['Alice', 26, 'A'])
print(student.items()) # Output: dict_items([('name', 'Alice'), ('age', 26),
('grade', 'A')])
Dictionaries are commonly used in Python to store and retrieve data based on
specific keys. They provide a flexible and efficient way to organize and
manipulate data, making them useful for various applications.
Python selection (conditional) statement’s
In Python, the if statement allows you to execute different blocks of code based
on different conditions. Apart from the basic if statement, there are three
additional forms of if statements that provide more flexibility in controlling the
flow of your program. Here's an explanation of all four forms of if statements in
Python:
Basic if statement:
The basic if statement allows you to execute a block of code if a condition is
true. It follows this syntax:
python
Copy code
if condition:
# Code block to execute if the condition is true
If the condition is true, the code block under the if statement is executed. If the
condition is false, the code block is skipped. Here's an example:
python
Copy code
age = 20
if age >= 18:
print("You are an adult.")
In this example, the code block print("You are an adult.") is executed because
the condition age >= 18 is true.
if-else statement:
The if-else statement allows you to execute one block of code if a condition is
true and another block of code if the condition is false. It follows this syntax:
python
Copy code
if condition:
# Code block to execute if the condition is true
else:
# Code block to execute if the condition is false
If the condition is true, the code block under the if statement is executed. If the
condition is false, the code block under the else statement is executed. Here's
an example:
python
Copy code
age = 15
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
In this example, because the condition age >= 18 is false, the code block
print("You are a minor.") is executed.
if-elif-else statement:
The if-elif-else statement allows you to test multiple conditions and execute
different blocks of code based on the first condition that evaluates to true. It
follows this syntax:
python
Copy code
if condition1:
# Code block to execute if condition1 is true
elif condition2:
# Code block to execute if condition1 is false and condition2 is true
else:
# Code block to execute if all conditions are false
The conditions are evaluated in order. If the first condition is true, its
corresponding code block is executed, and the rest of the elif and else blocks
are skipped. If none of the conditions are true, the code block under the else
statement is executed. Here's an example:
python
Copy code
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: D")
In this example, because the condition score >= 80 is true, the code block
print("Grade: B") is executed.
Nested if statement:
You can nest if statements within other if statements to create more complex
conditions.