Python 1
Python 1
Python PART 1
N
1.2 Python's History and Evolution
1.3 Python Installation
1.4 Writing Your First Python Program
O
Chapter 1: Introduction to Python
TR
In this chapter, we'll start with the basics of Python, a popular programming language. We'll
cover what Python is, its history, how to install it, and how to write your very first Python
program.
Python was created by Guido van Rossum and first released in 1991. It has since evolved and
grown into a powerful and easy-to-read language. Python is known for its simplicity and
ST
Before you can use Python, you need to install it on your computer. Here are the basic steps:
- Download the installer for your operating system (Windows, macOS, or Linux).
- Run the installer and follow the on-screen instructions.
Let's write a simple Python program that displays a message. Open a text editor (like Notepad
on Windows or TextEdit on macOS) and type the following code:
Python Code
print("Hello, Python!")
N
Now, open your computer's command prompt or terminal, navigate to the folder where you
saved your program, and type:
O
python first_program.py
You should see the message "Hello, Python!" printed on the screen. Congratulations, you've just
TR
run your first Python program!
That's it for the first chapter. You've learned what Python is, how it has evolved, how to install it,
and you've even written a simple program. As you continue your journey, you'll discover all the
amazing things you can do with Python.
EM
Chapter 2: Basic Syntax and Data Types
2.1 Indentation and Code Blocks
2.2 Variables and Data Types (int, float, str, bool)
2.3 Type Conversion
ST
In Python, we don't use curly braces like some other programming languages to define code
blocks. Instead, we use indentation. Indentation means the spaces or tabs at the beginning of a
line. It helps Python understand which lines of code belong together.
For example, in a loop or a function, you need to make sure that all the lines inside it are
indented by the same amount. Here's an example of a simple loop:
Python Code
for i in range(5):
print("This line is indented.")
print("So is this one.")
print("This line is not indented and is outside the loop.")
N
2.2 Variables and Data Types (int, float, str, bool)
O
Variables are like containers where you can store data. Python has different data types to
represent different kinds of information.
TR
- float : Used for numbers with decimals. Example: `price = 19.99`
Sometimes, you need to convert one data type into another. Python allows you to do this. For
example, you can convert an integer to a string like this:
Python Code
number = 42
ST
Comments are notes you can write in your code to explain what it does. Python ignores
comments when running your program. You can write a comment using the `#` symbol.
SY
Python Code
# This is a comment
Docstrings are comments that provide documentation about a function, class, or module. They
are enclosed in triple quotes. They are used to explain how to use a piece of code.
Python Code
def greet(name):
"""
This function greets the person passed in as a parameter.
"""
N
print("Hello, " + name)
In summary, in this chapter, you learned about the basics of Python's syntax, how to use
O
variables for different data types, how to convert between data types, and how to add comments
and docstrings to your code for better understanding and documentation.
TR
Chapter 3: Control Structures
3.1 Conditional Statements (if, elif, else)
3.2 Loops (for, while)
EM
3.3 Break and Continue Statements
3.4 Iterators and Generators
Conditional statements help your program make decisions. You can think of them like making
choices in your daily life. Here's how they work in Python:
Python Code
age = 18
if age >= 18:
print("You can vote!")
- elif statement: If the first condition is not true, check another condition.
Python Code
temperature = 25
if temperature > 30:
print("It's hot outside.")
N
elif temperature > 20:
print("It's a pleasant day.")
O
- else statement: If none of the conditions before it are true, do something else.
Python Code
grade = 60
TR
if grade >= 70:
print("You passed!")
else:
print("You didn't pass.")
- for loop: You can use it to go through a list of items or repeat an action a specific number of
times.
Python Code
for number in range(1, 6):
ST
print(number)
- while loop: Use it when you want to repeat something until a condition is no longer true.
SY
Python Code
count = 1
while count <= 5:
print("Hello!")
count += 1
This will print "Hello!" five times.
N
- break: It allows you to exit a loop early if a certain condition is met.
Python Code
numbers = [1, 2, 3, 4, 5]
O
for number in numbers:
if number == 3:
break
print(number)
TR
This will print 1 and 2 and then stop.
- continue: It allows you to skip the rest of the loop and move to the next iteration.
Python Code
EM
numbers = [1, 2, 3, 4, 5]
for number in numbers:
if number == 3:
continue
print(number)
These are like special loops and make working with large sets of data easier.
- Iterators: You can use them to go through items one at a time without loading everything into
memory.
SY
Python Code
my_list = [1, 2, 3]
my_iterator = iter(my_list)
print(next(my_iterator))
This will print 1 and then you can continue using `next()` to get the next item.
- Generators: They're like iterators but easier to create and read. They generate values one at
a time when you need them.
Python Code
def my_generator():
N
yield 1
yield 2
yield 3
O
gen = my_generator()
print(next(gen))
TR
This will also print 1 and then you can use `next()` to get the next values.
That's the basic idea of Control Structures in Python. They help you make decisions, repeat
tasks, and control the flow of your code.
EM
Chapter 4: Functions
4.1 Function Definition and Syntax
4.2 Function Parameters
4.3 Return Statements
4.4 Scope and Lifetime of Variables
4.5 Lambda Functions
ST
Chapter 4: Functions
SY
In Python, a function is like a mini-program that you can create and use whenever you want. It's
a way to group a bunch of code together to perform a specific task.
Python Code
def greet():
print("Hello, world!")
N
4.2 Function Parameters
Functions can take input values called parameters or arguments. These values allow your
O
function to be more flexible and do different things based on what you provide.
TR
Python Code
def greet(name):
print(f"Hello, {name}!")
greet("Alice")
greet("Bob")
EM
4.3 Return Statements
Functions can also give you back a result or answer using the `return` statement. This result can
be used in your program.
Python Code
ST
sum = add(3, 4)
print("The sum is:", sum)
SY
Variables inside functions have a scope, which means they only exist and can be used within
that function. Once the function finishes, the variables disappear.
N
my_function()
# You can't access x here because it's inside the function.
O
4.5 Lambda Functions
Lambda functions are small, anonymous functions. They can have any number of arguments
but can only have one expression. They are often used when you need a simple function for a
TR
short period.
Python Code
# Define a lambda function to double a number
double = lambda x: x * 2
EM
result = double(5)
print("Double of 5 is", result)
In summary, functions are like custom tools that you can create to make your code more
organized and reusable. You can give them inputs (parameters) and get results (return values).
Understanding how variables work inside functions and using lambda functions can make your
code more powerful and efficient.
ST
5.2 Tuples
5.3 Dictionaries
5.4 Sets
5.5 List Comprehensions
Chapter 5: Data Structures
Data structures in Python are like containers that help you store and organize your data. They
are like different types of boxes to hold things. In this chapter, we'll explore five important data
structures: Lists, Tuples, Dictionaries, Sets, and List Comprehensions.
N
5.1 Lists
- What is a List? A list is like a shopping list where you can write down multiple items in order.
O
In Python, a list is a collection of items enclosed in square brackets `[ ]`.
TR
Python Code
fruits = ["apple", "banana", "cherry"]
5.2 Tuples
- What is a Tuple? A tuple is similar to a list, but once you create it, you can't change its
EM
contents. It's like a sealed bag of items. In Python, a tuple is enclosed in parentheses `( )`.
Python Code
coordinates = (3, 4)
5.3 Dictionaries
ST
- What is a Dictionary? A dictionary is like a real dictionary with words and their meanings. In
Python, a dictionary is a collection of key-value pairs enclosed in curly braces `{ }`.
Python Code
SY
5.4 Sets
- What is a Set? A set is like a collection of unique items. You can't have duplicates. In Python,
a set is enclosed in curly braces `{ }` or created using the `set()` function.
- Example: A set of colors:
Python Code
colors = {"red", "green", "blue"}
N
5.5 List Comprehensions
- What is List Comprehension? List comprehension is a fancy way to create lists. It's like a
O
shortcut to make lists in just one line instead of using a loop.
TR
Python Code
even_numbers = [x for x in range(1, 11) if x % 2 == 0]
So, these data structures help you work with data more efficiently. Lists and tuples are for
ordered collections, dictionaries are for key-value pairs, sets are for unique items, and list
comprehensions are for creating lists quickly. You can use these structures to solve different
EM
types of problems in Python.
Questions 👍
ST
N
loops.
3. How can you control the flow of a loop using "break" and "continue" statements?
4. Explain the concept of iterators and provide a use case for iterators in Python.
5. Write a Python program that counts the number of even numbers in a list.
O
Chapter 4: Functions
TR
2. How do you pass parameters to a function, and what are the different ways to return values
from a function?
3. Describe the concept of variable scope in Python functions.
4. What is a lambda function, and how is it different from a regular function?
5. Write a Python function that calculates the factorial of a number.
These questions should help you assess your knowledge and comprehension of the topics
covered in the first six chapters of Python and OOP concepts.
SY