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

Chapter 5 Python All Notes

The document provides a comprehensive overview of programming concepts, focusing on the need for programs, control structures, and the Python programming language. It covers topics such as coding, data types, operators, and error handling, along with practical examples and exercises. Additionally, it explains how to install Python and interact with it using different modes.

Uploaded by

vidhi.sisn01179
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 views33 pages

Chapter 5 Python All Notes

The document provides a comprehensive overview of programming concepts, focusing on the need for programs, control structures, and the Python programming language. It covers topics such as coding, data types, operators, and error handling, along with practical examples and exercises. Additionally, it explains how to install Python and interact with it using different modes.

Uploaded by

vidhi.sisn01179
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

Part 2-

PROGRAM-
The step by step instructions written in any
programming language to do a specific task
is known as a program

WHY WE NEED A PROGRAM?


1. Understanding the problem-

--We need to understand the main objective


of the problem
--We should clearly define the problem
--Gather all necessary requirements
--Clarify goals
2. Analysing the problem-
--Break down the problem into smaller
parts
--Identify inputs and outputs
--Explore existing solutions
[Link] the Solution-
--Design an algorithm & draw a flowchart
--Choose appropriate tools & technologies
[Link] & Implementation
-- Any one computer language is used
-- Instruction of an algorithm is converted
into a computer understandable instruction

CONTROL STRUCTURES-
--These are the set of instructions that
control the flow of instructions in a
program.
-- These determines the order of execution
of the statements in any programming
language

TYPES OF CONTROL STRUCTURES-


(i) Sequential flow (ii) Selection Flow
(iii) Repetition flow

SEQUENTIAL FLOW-
--This is the default control structure where
statements are executed one after another
in the exact order they appear in the code.
SELECTION OR CONDITIONAL FLOW-
--These structures allow the program to
make decisions and choose between
different paths of execution based on
whether a condition is true or false.
--These structures can be of three types:
• Single Alternative- This structure has
the form:
If (condition) then:
[Module A]
• Double Alternative- This structure has
the form:
If (Condition):
[Module A]
Else:
[Module B]
Multiple Alternatives- This structure has
the form:
If (condition A):
[Module A]
Else if (condition B):
[Module B]
--
--
Else if (condition N):
[Module N]
REPETITION OR ITERATIVE FLOW
--These structures repeat a block of code
multiple times as long as a specified
condition remains true or for a fixed number
of iterations.
--Ex-
(i) for loop: Executes a block of code a
predetermined number of times.
(ii) while loop: Executes a block of code
repeatedly as long as a condition is true

PYTHON-
--It is a general purpose , object oriented,
easy to learn and high level programming
language.
-- Designed by Guido Von Rossom
-- released in 1991
INSTALLING PYTHON-
--Open the official website –
[Link].
-- Click on Download python.
--Click on the setup file to start installation.
--The Python Setup wizard appears
-- Click on the Install Now button
--After the setup progress is complete, the
message “Setup was successful” will be
displayed.

PART 3
WORKING IN PYTHON-

We can interact with Python IDLE in two different modes- Interactive mode & script
mode.

Python Character set

◼ Digits like 0,1,2….9.

◼ Letters in upper or lower case like A to Z or a to z.

◼ Special symbols like $, %, ^, *,(), @, ! etc.

◼ White spaces like spacebar, tab key or enter key.

Statements in Python
◼ A statement is a complete instruction that the Python interpreter can execute to
perform an action.

◼ There are 3 types of statements in Python-

Multiline Statements, Multiple statements, simple statements.

◼ Simple statements- These are the smallest unit of execution that do not
contain any logical or conditional expressions. Each statement is written on a
new line. Eg-

a=5

c=a+b

◼ Multiline statement- We use these statements when a single logical line of


code is too long. Python breaks long code lines using Backslashes (\) or by
wrapping then in brackets (), [], {}.

1. a=4+5+\

6+7+\

8+9

2. flowers= [‘Rose, Lily’,

‘Sunflower’ , ‘Hibiscus’]

3. marks = (10,25,16,

18,28,30)

◼ Multiple statements- These statements represent more than one statement


in a single line. This can be done by separating the statements using
semicolon (;).

Eg- a = 5 ; b = 10 ; c = a+b ; print (c)

TOKENS-

--A token is the smallest individual unit in a python program. All statements and
instructions in a program are built with tokens.

-- The various tokens in python are:


1. Keywords:

-- Keywords are words that have some special meaning or significance in a


programming language.

-- They can't be used as variable names, function names, or any other random purpose.

--In Python we have 33 keywords some of them are:

try, False, True, class, break, continue, and, as, assert, while, for, in, raise, except, or, not,
if, elif, print, import, etc.

2. Identifiers:

--Identifiers are the names given to any variable, function, class, list, methods, etc. for
their identification.

--Python is a case-sensitive language and it has some rules and regulations to name an
identifier.

RULES TO NAME AN IDENTIFIER:-

• Python is case-sensitive. So case matters in naming identifiers. And


hence geeks and Geeks are two different identifiers.

• Identifier starts with a capital letter (A-Z) , a small letter (a-z) or an underscore( _
). It can't start with any other character.
• Except for letters and underscore, digits can also be a part of identifier but can't
be the first character of it.

• Any other special characters or whitespaces are strictly prohibited in an


identifier.

• An identifier can't be a keyword.

3. Literals-

--It is defined as any data stored in a variable .

-- It is a constant whose value never change during the program execution.

-- Different types of literals in Python are-

(i) String Literals:

--The text written in single, double, or triple quotes represents the string literals in
Python.

--For example: "Computer Science", 'sam', etc. We can also use triple quotes to write
multi-line strings.

# String Literals

a = 'Hello'

b = "Geeks"

c = '''Geeks for Geeks is a

learning platform'''

(ii) Character Literals:

--character is enclosed in single or double-quotes.

a = 'G'

b = "W"
iii) Numeric Literals:

--These are the literals written in form of numbers.

--Python supports the following numerical literals:

• Integer Literal: It includes both positive and negative numbers along with 0. It
doesn't include fractional parts.

• Float Literal: It includes both positive and negative real numbers. It also
includes fractional parts.

• Complex Literal: It includes a+bi numeral, here a represents the real part and b
represents the complex part.

a=5

b = 10.3

c = -17

(iv) Boolean Literals: Boolean literals have only two values in Python. These are True
and False.

# Boolean Literals

a=3

b = (a == 3)

c = True + 10

(v) None literals : Only None is a special literal. It means something not yet created

PART 4
Date- 13.10.25

Punctuators-

--Special symbols to organize statements and expressions.

--example-

‘ ‘’ # \ ( ) { } [ ] @ , : . =

Operators-
-- Special symbols used to perform mathematical, logical and relational operators on
variables and values.

operand operator

Types of operators-

i. Arithmetic Operators

ii. Relational Operators

iii. Logical operators

iv. Assignment Operators

1. Arithmetic Operators-
Precedence of Arithmetic Operators-

To evaluate expressions involving several different types of operators, Python assigns an


order for which an operator must be evaluated first, which operator will be solved
second, and so on. This is called the precedence of arithmetic operators.
The precedence order is:

• Parenthesis

• Exponentiation

• Multiply and divide

• Add and subtract

Questions-

Q1. Evaluate-

1. 5 +10*2 – 3.

2. 2**3*4 + 5

3. (5 + 6) * 2

4. (4 + 3 % 5)

Q2. What are the values of the following Python expressions?

print(2**(3**2))

print((2**3)**2)

print(2**3**2)

PART 5
Date- 27.10.25

RELATIONAL OPERATORS or COMPARISON OPERATORS-


--Comparison operators (or Relational) in Python allow you to compare two values and return
a Boolean result: either True or False

1. Equality Operator (==)

--It checks if two values are exactly the same.

a=9

b=5
c=9
print(a == b)

print(a == c)

Output

False

True
2. Inequality Operator (!=)

--It checks if two values are not equal.

a=9

b=5

c=9

print(a != b)
print(a != c)

Output

True

False

3. Greater Than Operator (>)

4. Less Than Operator (<)

5. Greater Than or Equal To Operator (>=)

6. Less Than or Equal To Operator (<=)

LOGICAL OPERATORS-
ASSIGNMENT OPERATORS-

DATA TYPES-

--Data types specify the type of data that can be stored inside a variable. For example,

num = 24

Here, 24 (an integer) is assigned to the num variable. So the data type of num is of
the int class
Getting the Data Type

--You can get the data type of any object by using the type() function:

Example-

Print the data type of the variable x:

x=5
print(type(x))
Output-
<class 'int'>
How to check in the system-

Open IDLE (Interactive mode)

Write-

>>>x = "Hello World"

>>>print(x)

Output-

Hello World
>>>print(type(x))

Output-

<class 'str'>

DATA TYPE CONVERSION OR TYPE CASTING-


--It means changing the data type of a value.

-- For example, converting an integer (5) to a float (5.0) or a string ("10") to an integer (10).

-- In Python, there are two types of type conversion:

1. Implicit Conversion: Python changes the data type by itself while running the code,
to avoid mistakes or data loss.

2. Explicit Conversion: You change the data type on purpose using functions like int(),
float() or str().
Implicit Type Conversion

--Python automatically converts one data type into another during expression
evaluation.

--This usually happens when a smaller data type like int is combined with a larger
one like float in an operation.

Example:

Output
x: <class 'int'>
y: <class 'float'>
z = 20.6
z : <class 'float'>

Explicit Type Conversion


--When you manually convert the data type of a value using Python’s built-in
functions.

--Some common type casting functions include:


• int() converts a value to an integer
• float() converts a value to a floating point number
• str() converts a value to a string
• bool() converts a value to a Boolean (True/False)

Example:
s = "100" # String
a = int(s)
print(a)
print(type(a))
output-
100
<class 'int'>

COMMENTS IN PYTHON-

-- Comments can be used to explain Python code.

Single Line comment-

-- Comments starts with a #, and Python will ignore them:

Example-

#This is a comment.
print("Hello, World!")

Output-
Hello, World!

--Comments can be placed at the end of a line, and Python will ignore the rest of the line:

Example-
print("Hello, World!") #This is a comment.

Output-
Hello, World!

-- A comment does not have to be text that explains the code, it can also be used to prevent
Python from executing code:

Example-

#print("Hello, World!")
print("Cheers, Mate!")

Output-

Cheers, Mate!

Multi Line comment-

--Python does not really have a syntax for multiline comments.

--To add a multiline comment you could insert a # for each line:
Example-

#This is a comment
#written in
#more than just one line
print("Hello, World!")

Output-

Hello, World!

PART 6
ERRORS IN PYTHON-
--Errors are problems in a program that causes the program to stop its execution.

Syntax Error-

Syntax error occurs when the code doesn't follow Python's rules, like using incorrect
grammar in English. Python stops and points out the issue before running the program.

Example 1 :

In this example, this code returns a syntax error because there is a missing colon (:) after the
if statement. The correct syntax requires a colon to indicate the start of the block of code to be
executed if the condition is true.

a = 10000

if a > 2999

More Examples-
1. a+b=c
2. a=5
b=10
C=a+b
3. print(“python”))
Logical Error-
-- This kind of error is difficult to find since the program will run correctly but the desired
output is not achieved.

-- This happens if we give a wrong formula for the claculation to be done, write wrong logic
for the problem to be solved through code.

Examples-
[Link] calculate the average-
P= marks1+marks2/2
[instead of (marks1+marks2)/2]

[Link] find the perimeter of the rectangle-


P=2*l+b
[instead of 2*(l+b)]

Run Time Error-


--A runtime error in a program is an error that occurs while the program is running after
being successfully compiled.

--These errors can halt the program’s execution unexpectedly making them challenging to
debug.

A=int(input(“enter 1st no”))

B= int(input(“enter 2nd no”))


C=A/B

If A=5 and B=0, then it will display


zerodivisionError: division by zero

PART 7
PROGRAMS

Q1-WAP to input the first name and


last name from the user and display
the full name.
Ans-
first_name = input("Enter your First
name: ")
surname = input("Enter your Surname:
")

full_name = first_name + " " + surname

print("Hello " + full_name)


[Link] to input the length and
breadth of a rectangle. Calculate the
area and perimeter.
Ans-
length = int(input("Enter the length of
the rectangle: "))
breadth = int(input("Enter the breadth
of the rectangle: "))
area = length * breadth
perimeter = 2 * (length + breadth)
print("The area of the rectangle
is:”,area)
print("The perimeter of the rectangle
is:",perimeter)

[Link] to input the percentage of a


student and award “Certificate of
Excellence” if the student gets more
than 80%
Ans-

percent=int(input(“Enter your
percentage”))
if percent>80:
print(“Congratulations”)
print(“You are awarded-’
Certificate of Excellence’ ”)

[Link] to input day of the week and


in case it is SUNDAY then extra
cookie as a reward point to be given
with the order.

Ans-
weekday=input (“Enter day of the
week:”)
if weekday ==”SUNDAY”:
print (“Reward Point -Chocolate
Cookie FREE !”)

Q4. WAP to input two numbers and


display the bigger of the two
numbers.
Ans-
N1= int(input(“Enter 1st number:”))
N2= int(input(“Enter 2nd number:”))
if N1>N2:
print(N1, “ is bigger”)
else:
print( N2,” is bigger”)

You might also like