0% found this document useful (0 votes)
2 views7 pages

Chapter 1 Master

Chapter 1 introduces Python programming, highlighting its simplicity, versatility, and extensive libraries for various applications. It covers fundamental concepts including data types, conditional statements, loops, functions, file handling, graphics, and key libraries like NumPy and Matplotlib. The chapter provides code examples to illustrate these concepts and their practical applications.

Uploaded by

lalouiyoucef50
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Chapter 1 Master

Chapter 1 introduces Python programming, highlighting its simplicity, versatility, and extensive libraries for various applications. It covers fundamental concepts including data types, conditional statements, loops, functions, file handling, graphics, and key libraries like NumPy and Matplotlib. The chapter provides code examples to illustrate these concepts and their practical applications.

Uploaded by

lalouiyoucef50
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter 1: Introduction to Python Programming

Chapter 1: Introduction to Python Programming

Table of Contents

1. Introduction to Python

2. Data Types and Expressions

3. Conditional Statements

4. Repetitive Instructions (Loops)

5. Functions and Procedures

6. File Handling (Reading and Writing)

7. Graphics

8. Libraries (NumPy, Matplotlib)

1|P a g e
Chapter 1: Introduction to Python Programming

1. Introduction to Python

Python is a high-level, versatile programming language known for its readability and simplicity. It

has become one of the most popular languages for software development, data analysis, web

development, automation, and scientific computing. Python’s vast ecosystem of libraries makes it ideal

for a wide variety of applications.

Key Features of Python:

• Simple and Readable Syntax: Python’s syntax is designed to be intuitive and close to natural

language, making it easy to write and read.

• Interpreted Language: Python executes code line by line, without the need for compiling,

which makes debugging and prototyping faster.

• Cross-Platform: Python runs on various operating systems like Windows, MacOS, and

Linux.

• Large Standard Library: Python includes a vast standard library that handles file I/O, data

manipulation, and many other utilities.

2. Data Types and Expressions

Python has several built-in data types that can store different kinds of values, such as numbers, text,

and logical values. Data types define how the stored data is used and manipulated.

Key Data Types:

• Integers (int): Whole numbers (e.g., 5, -10).

• Floats (float): Numbers with decimals (e.g., 3.14, 0.001).

• Strings (str): Text data enclosed in quotes (e.g., "Python").

• Booleans (bool): Logical values True or False.

Expressions:

2|P a g e
Chapter 1: Introduction to Python Programming

Expressions are operations that manipulate data and return new values. Common operations include:

• Arithmetic: +, -, *, /, // (floor division), ** (exponentiation), % (modulus).

• Relational: >, <, >=, <=, ==, !=.

• Logical: and, or, not.

Example 1:
Python code

# Example of data types and expressions


x = 10 # Integer
y = 3.14 # Float
z = "Python" # String
is_active = True # Boolean
# Arithmetic and string operations
result = x * y
print(result) # Output: 31.4
print(z + " is fun!") # Output: Python is fun!

3. Conditional Statements

Conditional statements control the flow of a program by allowing certain blocks of code to be

executed based on conditions. The if, elif, and else keywords allow programs to make decisions.

Key Points:

• if: Executes a block of code if the condition is True.

• elif: Specifies additional conditions to check if the previous if condition was False.

• else: Executes a block of code if none of the conditions are met.

Example 2:
Python code

age = 20
if age >= 18:
print("You are an adult.")
elif age >= 13:
print("You are a teenager.")
3|P a g e
else:
print("You are a child.")
Chapter 1: Introduction to Python Programming

4. Repetitive Instructions (Loops)

Loops allow us to execute a block of code repeatedly, either for a specific number of iterations or

while a condition holds true. Python supports two main types of loops: for and while.

• For Loop: Used for iterating over a sequence like a list, tuple, or range.

Example 3:
Python code

# For loop example


for i in range(5):
print(i) # Output: 0 1 2 3 4

• While Loop: Repeats a block of code as long as the condition is True.

Example 4:
Python code

# While loop example


count = 0

while count < 5:


print("Count is:", count)
count += 1 # Increment count by 1

5. Functions and Procedures

Functions are reusable blocks of code that perform specific tasks. They help in organizing code,

making it modular and easy to maintain.

Defining a Function:

A function is defined using the def keyword, followed by the function name, parameters, and the

function body. Functions can accept input arguments and return a value using the return keyword.

4|P a g e
Chapter 1: Introduction to Python Programming

Example 5:
Python code

z=5
def add(a, b):
return a + b
y = add(5, 3)
result =y+z
print(result) # Output: 13

Local and Global Variables:

Variables inside a function are local to that function, while variables outside a function are

global and can be accessed anywhere in the program.

6. File Handling (Reading and Writing)

Python can handle external files, allowing you to read data from and write data to files. This is

particularly useful for working with large datasets or saving program results.

File Modes:

• r: Read mode (opens the file for reading).

• w: Write mode (opens the file for writing, overwrites if the file exists).

• a: Append mode (adds content to the end of the file).

Example 6:
Python code

# Writing to a file
with open("[Link]", "w") as file:
[Link]("Hello, Python!")

# Reading from a file


with open("[Link]", "r") as file:
content = [Link]()
print(content)
5|P a g e
# Output: Hello, Python!
Chapter 1: Introduction to Python Programming

7. Graphics

Python allows for graphical data visualization using libraries like matplotlib. Visualizing data is

crucial for analyzing and interpreting numerical results.

Matplotlib: matplotlib is a popular Python library used to generate plots and charts.

Important note: Steps to Install Matplotlib.

• Open Command Prompt: Press Win + R, type cmd, and hit Enter.

• Install Matplotlib: Run the following command to install Matplotlib pip install

matplotlib

Example 7:
Python code

import [Link] as plt # Import the [Link] module for plotting


x = [1, 2, 3, 4] # Define the x-coordinates of the data points
y = [10, 20, 25, 30] # Define the y-coordinates of the data points
[Link](x, y) # Create a line plot using the x and y data
[Link]('X-axis') # Label the x-axis
[Link]('Y-axis') # Label the y-axis
[Link]('Simple Line Plot') # Set the title of the plot
[Link]() # Display the plot in a window

8. Libraries (NumPy, Matplotlib)

Python’s power in scientific computing comes from its extensive libraries. Three key libraries for

numerical methods and data visualization are:

• NumPy: A fundamental package for numerical computing, particularly useful for handling

arrays and matrices.

6|P a g e
Chapter 1: Introduction to Python Programming

• Matplotlib: A library used for creating static, animated, and interactive visualizations in

Python.

Example 8:
Python code

import numpy as np
import [Link] as plt
# Create an array using NumPy
x = [Link](0, 10, 100) # Array of 100 points from 0 to 10
y = [Link](x) # Sine of each point
# Plotting using Matplotlib
[Link](x, y)
[Link]("Sine Wave")
[Link]("X-axis")
[Link]("Y-axis")
[Link]()
[Link]() # Display the plot

7|P a g e

You might also like