26BTAD142G-PROBLEM SOLVING USING PYHTON
UNIT-1
Elements of Python:
Python is a high-level programming language. It was created by Guido van
Rossum and first released in 1991. Python is used to develop websites,
software, games, data analysis, artificial intelligence, and many other
applications.
Python uses simple English-like words, so it is easy for beginners to
understand and write programs.
The main elements of Python are given below.
1. Variables
A variable is used to store data or information.
Example:
name = "Ravi"
age = 20
print(name)
print(age)
Here, name stores "Ravi" and age stores 20.
2. Data Types
A data type tells us what type of data is stored in a variable.
Some common data types are:
Integer (int) – Whole numbers, such as 10
Float (float) – Decimal numbers, such as 10.5
String (str) – Text, such as "Hello"
Boolean (bool) – True or False
Example:
age = 20
print(age)
height = 5.8
print(height)
name = "Ravi"
print(name)
student = True
print(student)
3. Operators
Operators are symbols used to perform operations on data.
Example:
a = 10,b = 5
print(a + b)
print(a - b)
print(a * b)
Output:
15
5
50
4. Input
The input() function is used to get information from the user.
Example:
name = input("Enter your name: ")
print("Hello", name)
If the user enters Ravi, the output will be: Hello Ravi
5. Conditional Statements
Conditional statements are used to make decisions in a program.
The main keywords are if, elif, and else.
Example:
age = 18
if age >= 18:
print("You are an adult")
else:
print("You are a child")
Here, Python checks the condition and gives the correct result.
6. Loops
A loop is used to repeat a set of instructions.
Example:
for i in range(3):
print("Hello")
Output:
Hello
Hello
Hello The loop prints "Hello" three times.
7. Functions
A function is a block of code used to perform a particular task.
Example:
def greet():
print("Good Morning")
greet()
Output:
Good Morning Here, greet() is a function.
8. Lists
A list is used to store multiple values in one variable.
Example:
fruits = ["Apple", "Banana", "Mango"]
print(fruits)
Output: ['Apple', 'Banana', 'Mango']
9. Comments
Comments are used to write notes in a program. Python does not execute
comments. The # symbol is used for comments.
Example:
# This program prints a name
name = "Ravi"
print(name)
Variables:
A variable is a name used to store a value .
We can think of a variable as a box. We give the box a name and put some
information inside it. Later, we can use the name to get that information.
Simple Example
name = "Ravi"
age = 20
Here:
name is a variable.
"Ravi" is the value stored in the variable.
age is a variable.
20 is the value stored in the variable.
We can display the values using print().
name = "Ravi"
age = 20
print(name)
print(age)
Output:
Ravi
20
How to Create a Variable
We simply give a name and assign a value using the = symbol.
Syntax
variable_name = value
Example
city = "Chennai"
Here, city is the variable and "Chennai" is its value.
Another example:
marks = 85
Here, marks is the variable and 85 is its value.
Types of Values in Variables
A variable can store different types of values.
1. String
A string stores text.
name = "Ravi"
2. Integer
An integer stores a whole number.
age = 20
3. Float
A float stores a decimal number.
price = 99.50
4. Boolean
A Boolean stores either True or False.
is_student = True
Naming Rules for Variables
There are some simple rules for naming variables:
1. A variable name can contain letters, numbers, and underscore (_).
2. A variable name cannot start with a number.
3. A variable name cannot contain spaces.
4. Python variable names are case-sensitive.
5. We should use meaningful names.
Correct Examples
name = "Ravi"
student_age = 20
total_marks = 450
Incorrect Examples
2name = "Ravi" # Cannot start with a number
student age = 20 # Cannot contain spaces
Case-Sensitive Variables
Python treats uppercase and lowercase letters as different.
name = "Ravi"
Name = "Kumar"
print(name)
print(Name)
Output:
Ravi
Kumar
name and Name are two different variables.
Multiple Variables
Python allows us to create multiple variables in one line.
name, age, city = "Ravi", 20, "Chennai"
print(name)
print(age)
print(city)
Output:
Ravi
20
Chennai
Data Types :
A data type tells us what type of data we are storing.
For example:
name = "Ravi"
age = 20
Here, "Ravi" is text and 20 is a number.
Python has different types of data.
1. Integer (int) :An integer is a whole number.
Example:
age = 20
Here, 20 is an integer.
2. Float (float):A float is a decimal number.
Example:
height = 5.8
Here, 5.8 is a float.
3. String (str) :A string is text.
It is written inside quotes.
Example:
name = "Ravi"
Here, "Ravi" is a string.
4. Boolean (bool) :Boolean has only two values.
They are:
True
False
Example:
is_student = True
Here, True means yes.
Another example:
is_teacher = False
Here, False means no.
5. List (list) :A list stores many values.
It uses square brackets [ ].
Example:
fruits = ["Apple", "Banana", "Mango"]
Here, three fruits are stored in one list.
6. Tuple (tuple) :A tuple also stores many values.
It uses round brackets ( ).
Example: colors = ("Red", "Green", "Blue")
A tuple is normally not changed after it is created.
7. Set (set) :A set stores different values.
It does not keep duplicate values.
Example: numbers = {10, 20, 30}
If we give the same value twice, Python keeps it only once.
numbers = {10, 20, 20, 30}
Result:
{10, 20, 30}
8. Dictionary (dict)
A dictionary stores data as key and value.
Example:
student = {
"name": "Ravi",
"age": 20
}
Here:
name is the key.
Ravi is the value.
age is the key.
20 is the value.
Assignment Statement :
An assignment statement is used to give a value to a variable.
The = symbol is called the assignment operator.
Simple Example
age = 20
Here:
age is the variable.
20 is the value.
= gives the value 20 to age.
Example 1
name = "Ravi"
Here, "Ravi" is stored in the variable name.
Multiple Assignment
Python allows us to give values to many variables in one line.
name, age = "Ravi", 20
print(name)
print(age)
Output:
Ravi
20
Type Conversion :
Type conversion means changing one data type into another data type.
For example, we can change an integer into a float or a string into an integer.
Simple Example
x = 10
y = float(x)
print(y)
Output:
10.0
Here, the integer 10 is changed into a float 10.0.
1. Integer to Float
We can change an integer into a float using float().
x = 10
y = float(x)
print(y)
Output:
10.0
2. Float to Integer
We can change a float into an integer using int().
x = 10.5
y = int(x)
print(y)
Output:
10
Arithmetic Operators :
Arithmetic operators are used to do mathematical calculations in Python.
Example
a = 10
b=5
print(a + b)
Output:
15
Here, + is an arithmetic operator.
Types of Arithmetic Operators
Python has the following arithmetic operators:
Operator Meaning Example Answer
+ Addition 10 + 5 15
- Subtraction 10 - 5 5
* Multiplication 10 * 5 50
/ Division 10 / 5 2.0
% Modulus 10 % 3 1
// Floor Division 10 // 3 3
** Power 2 ** 3 8
1. Addition (+)
The + operator is used to add two numbers.
a = 10
b=5
print(a + b)
Output:
15
2. Subtraction (-)
The - operator is used to subtract one number from another.
a = 10
b=5
print(a - b)
Output:
5
3. Multiplication (*)
The * operator is used to multiply two numbers.
a = 10
b=5
print(a * b)
Output:
50
4. Division (/)
The / operator is used to divide one number by another.
a = 10
b=5
print(a / b)
Output:
2.0
5. Modulus (%)
The % operator gives the remainder after division.
a = 10
b=3
print(a % b)
Output:
1
Operator Precedence :
Operator precedence tells Python which operation to do first when there is
more than one operator in an expression.
👉 Operator precedence means the order of calculation.
Simple Example
result = 10 + 5 * 2
print(result)
Output:
20
Python does multiplication first:
5 * 2 = 10
10 + 10 = 20
So, the answer is 20.
Order of Operators:
Python generally follows this order:
1. () → Parentheses
2. ** → Power
3. *, /, //, % → Multiplication and division
4. +, - → Addition and subtraction
Easy Way to Remember
Brackets → Power → Multiply/Divide → Add/Subtract
1. Parentheses ()
Parentheses are done first.
result = (10 + 5) * 2
print(result)
Output:
30
First:
10 + 5 = 15
Then:
15 * 2 = 30
2. Power **
Power is done before multiplication and addition.
result = 2 + 3 ** 2
print(result)
Output:
11
First:
3 ** 2 = 9
Then:
2 + 9 = 11
3. Multiplication and Division
Multiplication and division are done before addition and subtraction.
result = 10 + 5 * 2
print(result)
Output:
20
First:
5 * 2 = 10
Then:
10 + 10 = 20
4. Addition and Subtraction
Addition and subtraction have lower priority than multiplication and division.
result = 20 - 10 + 5
print(result)
Output:
15
Here, the operations are done from left to right:
20 - 10 = 10
10 + 5 = 15
Example with Multiple Operators
result = 10 + 5 * 2 - 4
print(result)
First, multiplication:
5 * 2 = 10
Now:
10 + 10 - 4
Then work from left to right:
10 + 10 = 20
20 - 4 = 16
Output:
16
Using Parentheses
We can use parentheses when we want a different order.
result = (10 + 5) * 2
print(result)
Output:
30
Without parentheses:
result = 10 + 5 * 2
Output:
20
So, parentheses can change the order of calculation.
Expression :
An expression is a combination of values, variables, and operators.
An expression gives a result.
Simple Example:10 + 5
Here:
10 is a value.
+ is an operator.
5 is a value.
The result is:
15
So, 10 + 5 is an expression.
Example 1: Addition
a = 10
b = 20
result = a + b
print(result)
Output:
30
Here, a + b is an expression.
Expression Evaluation:It means calculating an expression to get the answer.
Example of Expression Evaluation
a = 10
b=2
result = a * b + 5
print(result)
Step-by-step evaluation:
First, Python gets the values:
a = 10
b=2
Then it calculates:
a*b+5
= 10 * 2 + 5
= 20 + 5
= 25
Output:
25
So, Python evaluates the expression a * b + 5 and gives the result 25.
Keywords :
Keywords are special words in Python.
Python already gives these words a fixed meaning.
We cannot use keywords as variable names.
Example
if age >= 18:
print("Adult")
Here, if is a keyword.
It is used to check a condition.
there are 35 reserved keywords. Common Python Keywords
Keyword
if
else
for
while
def
Conditional Statements:
A conditional statement is used to make a decision in a Python program.
Python mainly uses:
if
elif
else
In simple words:
Conditional statement :
It is used to Make a decision based on a condition.
A conditional statement checks a condition and performs an action based on
the result.
1. if Statement
An if statement executes a block of code when a condition is true.
The if statement is used to check a condition.
If the condition is true, Python runs the code inside if.
Syntax
if condition:
statement
Simple Example
age = 20
if age >= 18:
print("You can vote")
Output:
You can vote
2. elif Statement
elif means else if.
It is used to check another condition when the previous condition is false.
Syntax
if condition:
statement
elif condition:
statement
else:
statement
Simple Example
marks = 75
if marks >= 90:
print("A Grade")
elif marks >= 50:
print("B Grade")
else:
print("Fail")
Output:
B Grade
Here:
75 >= 90 is false.
75 >= 50 is true.
So Python prints B Grade.
3. Nested Conditional Statement
A nested conditional statement means putting one if statement inside
another if statement.
if inside another if = Nested if
Syntax
if condition:
if condition:
statement
Simple Example
age = 20
citizen = True
if age >= 18:
if citizen:
print("You can vote")
Output:
You can vote
Here:
1. Python first checks age >= 18.
2. If it is true, Python checks citizen.
3. Both conditions are true.
4. So it prints "You can vote".
Another Nested if Example
number = 10
if number > 0:
if number % 2 == 0:
print("Positive even number")
Output:
Positive even number
Here:
First, Python checks whether the number is positive.
Then, it checks whether the number is even.
Both conditions are true.
Loops :
A loop is used to repeat a block of code.
Loop = Repeat the same code.
Python has different types of loops. The main ones are:
1. for loop
2. while loop
3. Nested loop
1. For Loop
A for loop is used to repeat a statement for a fixed number of times
Syntax
for variable in sequence:
statement
Simple Example
for i in range(5):
print(i)
Output:
01234
Here, the loop runs 5 times.
Another Example
for i in range(3):
print("Hello")
Output:
Hello Hello Hello
2. While Loop
A while loop is used to repeat a statement while a condition is true.
Syntax
while condition:
statement
Simple Example
i=1
while i <= 5:
print(i)
i=i+1
Output:
12345
Here, the loop continues while i <= 5 is true.
3. Nested Loop
Definition
A nested loop means one loop inside another loop.
Syntax
for variable in sequence:
for variable in sequence:
statement
Simple Example
for i in range(2):
for j in range(3):
print(i, j)
Output:
00
01
02
10
11
12
Here, the second loop runs 3 times for each run of the first loop.
Break and Continue:
break → stops the loop
continue → skips the current step
1. Break Statement
Definition
The break statement is used to stop the loop completely.
Syntax
break
Simple Example
for i in range(1, 6):
if i == 3:
break
print(i)
Output:
1
2
Here, when i becomes 3, the break statement stops the loop.
2. Continue Statement
The continue statement is used to skip the current iteration and move to the
next iteration.
Syntax
continue
Simple Example
for i in range(1, 6):
if i == 3:
continue
print(i)
Output:
1
2
4
5
Here, when i becomes 3, Python skips 3 and continues with the next number.