Python notes
Python notes
Keywords in PSEUDOCODE:
INPUT – Get input from the user
COMPUTE – Calculate values by using other
variables. For Ex: Input n1 and n2. Compute result
= n1 + n2
PRINT – To print statements
INCREMENT
DECREMENT
IF / ELSE
WHILE
TRUE / FALSE
Repetition: WHILE < Condition >, Repeat < Steps >
Program to print the difference between two numbers:
Mapping:
Mapping is an unordered data type in Python.
Currently, there is only one standard mapping
data type in Python called dictionary.
Dictionary:
Dictionary in Python holds data items in key-
value pairs. Items in a dictionary are enclosed
in curly brackets {x}.
Mutable & Immutable Data types:
Variables whose values can be changed after they
are created and assigned are called mutable. Ex:
Integers, Float, Boolean, Complex, Strings and
Tuples
Variables whose values cannot be changed after
they are created and assigned are called
immutable. Ex: Lists, Sets and Dictionary.
When an attempt is made to update the value of
an immutable variable, the old variable is
destroyed and a new variable is created by the
same name in memory.
Usage of Datatypes:
It is preferred to use lists when we need a simple
iterable collection of data that may go for frequent
modifications.
Operators:
An operator is used to perform specific
mathematical or logical operations on values.
Types of operators:
o Arithmetic:
Relational:
Identity:
Augmental Assignment:
Logical:
Membership:
Precedence of operators:
Range () function:
range ( Start, Stop, Step )
Default start value = 0 & step value = 1
While loop:
Syntax:
While < condition >:
Body of the loop
Flowchart:
Break Statement:
The break statement alters the normal flow of
execution as it terminates the current loop and
resumes execution of the statement following
that loop.
Continue Statement:
When a continue statement is encountered, the
control skips the execution of remaining
statements inside the body of the loop for the
current iteration and jumps to the beginning of
the loop for the next iteration.
Break Statement & Continuous Statement = Jump
Statements
Python tokens:
Token: Smallest individual unit in a program
[ Lexical ]
Types:
o Keywords: Word with a special handling by
Python
o Literals: Datatypes with a fixed value
o Datatypes: Python’s capability to handle data
o Operators: Arithmetic, Relational, Logical,
Augmental Assignment, Identity and
Membership
o Punctuators: Symbols in Python to organize a
programming language
o Identifiers: Names given to different parts of
the program
Complex number:
Complex numbers are a composite quantity made
of two parts, a real part and also an imaginary
part.
Types of statements:
Empty [ OR ] Null statement:
Does nothing and moves to the next
statement
Example: Pass: To move to the next
statement
Simple Statement:
A single executable statement
Each line in the program is a simple
statement
Example: name = input (“Enter your name:
“)
Compound Statement:
A group of statements executed as a unit
Example: if (age>18):
print (“Eligible to vote”)
Statement flow of control:
Sequence:
Statements being executed sequentially
Example: name = input (“Your name: “)
print (name)
Selection:
Execution of statements depending upon the
condition
Example: if, if…else, if…elif…else, nested if
Repetition / Iteration / Looping:
Repetition of a set of statements depending
upon the condition
Example: for & while
If Statement:
Syntax:
if < Condition >:
Statement 1
Statement 2
Statement 3
Statement 4
Statement 5
Example: age = int (input (“Enter your age: “))
if age > 18: print (“Eligible to vote”)
Note: If you have only one statement under the if
statement, it can be written in the same line as
the if statement
If…Else Statement:
Syntax:
if < Condition >:
Statement 1
Statement 2
Statement 3
else:
Statement 4
Statement 5
Example: age = int (input (“Enter your age: “))
if age >= 18: print (“Eligible”)
else: print (“Not eligible”)
If…Elif…Else Statement:
Syntax:
if < Condition 1 >:
Statement 1
elif < Condition 2 >:
Statement 2
Statement 3
else:
Statement 4
Statement 5
Iteration / Looping Statement:
These statements allow a set of instructions to be
performed repeatedly until a certain condition is
fulfilled
Also known as looping statement or repetition.
2 Categories:
Counting loop:
1.The loops that repeat a certain number
of times
2.Example: For Loop
Conditional loop:
1.The loops that repeat as long as a
certain condition is True
2.Example: While Loop