๐ Revisiting Python
Python is a popular programming language used
to create computer programs, websites, games, applications,
artificial intelligence systems and many other digital solutions.
Python is known for its simple and readable syntax, which makes
it a useful language for beginners.
๐ป What is a Program?
A program is a set of instructions written to
make a computer perform a task.
A Python program is made up of statements that are executed by
the Python interpreter.
print("Hello, World!")
๐ Python Statements
An instruction written in a programming language
is often called a statement.
name = "Ananya"
print(name)
Python generally executes statements in the order in which they
are written, unless instructions such as conditions or loops
change the flow of the program.
๐ฆ Variables
A variable is a named location used to store
data in a program.
name = "Riya"
age = 12
marks = 95
The values stored in variables can be used later in the program.
๐ Updating a Variable
The value stored in a variable can be changed.
score = 10
score = 20
print(score)
The program displays the latest value stored in the variable.
โจ๏ธ Taking Input
The input() function is used to accept information
from the user.
name = input("Enter your name: ")
print("Hello", name)
The user's response can be stored in a variable.
๐ข Type Conversion
Data received through input() is generally treated
as text. When numerical input is required, type conversion may
be used.
age = int(input("Enter your age: "))
print(age)
Common conversion functions include:
int() โ Converts a value to an integer.
float() โ Converts a value to a decimal number.
str() โ Converts a value to text.
โ Operators in Python
Operators are symbols or keywords used to
perform operations on values.
Python uses different types of operators, including:
- Arithmetic operators
- Comparison operators
- Logical operators
๐งฎ Arithmetic Operators
Arithmetic operators are used to perform mathematical
calculations.
a = 10
b = 3
print(a + b)
print(a - b)
print(a * b)
print(a / b)
โ๏ธ Comparison Operators
Comparison operators compare two values and produce either
True or False.
marks = 85
print(marks > 50)
Common comparison operators include:
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
๐ Boolean Values
A Boolean value can have one of two values:
print(10 > 5)
print(10 < 5)
Boolean values are useful when a program needs to make decisions.
๐ค Decision-Making in Python
Sometimes a program must choose between different actions.
Python uses conditional statements to make decisions based on
whether a condition is True or False.
๐ฆ The if Statement
The if statement executes a block of code only
when a condition is true.
marks = 75
if marks >= 40:
print("You have passed.")
Notice the colon (:) and the indentation before
the print() statement.
๐ Indentation
Indentation means leaving spaces at the
beginning of a line of code.
Python uses indentation to identify which statements belong
to a particular block.
if age >= 18:
print("Eligible")
Incorrect indentation can cause an error.
๐ The if...else Statement
The else statement provides an alternative action
when the condition is false.
marks = 35
if marks >= 40:
print("Pass")
else:
print("Try again")
๐ฅ The if...elif...else Statement
The elif statement can be used to check additional
conditions.
marks = 78
if marks >= 90:
print("Excellent")
elif marks >= 60:
print("Good")
else:
print("Keep practising")
๐ What is a Loop?
A loop is used to repeat a set of instructions.
Loops help programmers avoid writing the same instructions again
and again.
๐ The for Loop
A for loop is commonly used when a task needs to
be repeated a known number of times.
for number in range(5):
print(number)
This loop displays numbers from 0 to 4.
๐ข The range() Function
The range() function is often used with a
for loop.
for number in range(1, 6):
print(number)
This displays:
1
2
3
4
5
๐ The while Loop
A while loop repeats instructions while a condition
remains true.
number = 1
while number <= 5:
print(number)
number = number + 1
โพ๏ธ Avoiding Infinite Loops
An infinite loop is a loop that continues
running because its condition never becomes false.
When using a while loop, ensure that the variables
involved in the condition are updated correctly.
๐ค Strings
A string is a sequence of characters used to
represent text.
name = "Python"
message = "Welcome to programming!"
Strings are written inside quotation marks.
๐ Joining Strings
Two or more strings can be joined together using the
+ operator.
first_name = "Aman"
last_name = "Sharma"
full_name = first_name + " " + last_name
print(full_name)
๐ Finding String Length
The len() function can be used to find the number
of characters in a string.
name = "Python"
print(len(name))
๐ Introduction to Lists
A list is used to store multiple values in a
single variable.
subjects = [
"English",
"Mathematics",
"Science",
"Computer"
]
Lists are written using square brackets
([ ]).
๐ Accessing List Items
Each item in a list has a position called an index.
Python list indexing begins at 0.
subjects = [
"English",
"Maths",
"Science"
]
print(subjects[0])
The output is:
English
โ Adding an Item to a List
The append() method adds an item to the end of a
list.
fruits = ["Apple", "Banana"]
fruits.append("Mango")
print(fruits)
๐ Looping Through a List
A for loop can be used to process each item in a
list.
fruits = [
"Apple",
"Banana",
"Mango"
]
for fruit in fruits:
print(fruit)
๐ Finding and Fixing Errors
The process of finding and correcting errors in a program is
called debugging.
Common programming errors include:
- Syntax errors
- Logical errors
- Runtime errors
๐ฌ Comments in Python
A comment is a note written in a program to
explain the code.
Python ignores comments while executing the program.
# This program displays a message
print("Hello!")