0% found this document useful (0 votes)
2 views

Python notes

The document outlines the characteristics of a good algorithm, input/output operations, and decision-making structures in Python. It covers various data types, including numeric, boolean, sequence, and mapping types, along with their properties and usage. Additionally, it explains control flow statements, loops, and the importance of operators and tokens in Python programming.

Uploaded by

orewairfan05
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python notes

The document outlines the characteristics of a good algorithm, input/output operations, and decision-making structures in Python. It covers various data types, including numeric, boolean, sequence, and mapping types, along with their properties and usage. Additionally, it explains control flow statements, loops, and the importance of operators and tokens in Python programming.

Uploaded by

orewairfan05
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

PYTHON – NOTES

Characteristics of a good algorithm:


 Precision
 Uniqueness
 Finiteness
 Input
 Output

Input / Output: Input, Print and variables like it


Decision: If…Elif…Else.. statement
Process: Variable assignment
Start / End: Start and End
Arrow: To connect from one function to another

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:

Types of execution modes:


 Interactive mode: The output is displayed in the
>>> prompt directly
 Script mode: Can write a Python program in a file,
save it and use the interpreter to execute it
Identifiers:
 Identifiers are names used to identify a program's
variable, function, or other entities.
 Rules for naming an identifier:
 Should begin with an uppercase or a
lowercase letter or an underscore sign [ _ ]
 Shouldn’t start with a digit
 Can be in any length
 Shouldn’t contain any keyword
 Shouldn’t have any special symbols
except underscore [ _ ]
Variables:
 Variable in Python refers to an object — an item or
element stored in the memory.
Comments:
 Start with a # and provide a comment
 This is called as a comment line

Numeric data types:

Boolean data type (bool) is a subtype of integer. It is a


unique data type, consisting of two constants, True
and False. Boolean True value is non-zero, non-null
and non-empty. Boolean False is the value zero.

Sequence data types:


 Strings: Enclosed in double or single quotes [ ‘x’
OR “x” ]
 List: A list is a sequence of items separated by
commas and enclosed in square brackets [x] and
it is changeable.
 Tuple: A Tuple is a sequence of items separated by
commas and items are enclosed in parenthesis
(x). This is unlike a list, where values are enclosed
in brackets [x]. Once created, we cannot change
the tuple and it is ordered.
Set:
 Set is an unordered collection of items separated
by commas and the items are enclosed in curly
brackets {x}. A set is similar to a list, except that
it cannot have duplicate entries. Once created,
elements of a set cannot be changed.
None:
 None is a special data type with a single value. It
is used to signify the absence of value in a
situation. None supports any special operations,
and it is neither the same as False nor 0 (zero).

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:

Type function conversion:


Types of errors:
Syntax error, Logical error & Runtime error
Repetition:
 FOR Loop:
 Syntax:
for < Control-variable > in < Sequence >:
< Body of the loop >
 Flowchart:

 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

You might also like