VKS Python
VKS Python
PYTHON
VKS-LEARNING HUB
What is Python…?
Python is an easy to learn, open-source, object-oriented,
general-purpose programming language.
Python is developed by Guido van Rossum in 1991.
Features of Python
1. Free and Open Source : It is freely available without any
cost. It is open source means its source-code is also available
which you can modify, improve .
2. Easy to use – Due to simple syntax rule
3. Interpreted language – Code execution & interpretation line
by line
4. Cross-platform language – It can run on windows, linux, mac
5. Expressive language – Less code to be written as it itself
express the purpose of the code.
6. Extensive libraries- have many built-in and external libraries
support
VKS-LEARNING HUB
Programming basics
• code or source code: The sequence of instructions in a program.
• syntax: The set of legal structures and commands that can be used in a
particular programming language.
• output: The messages printed to the user by a program.
• console: The text box onto which output is printed.
– Some source code editors pop up the console as an external window,
and others contain their own console window.
4
VKS-LEARNING HUB
5
VKS-LEARNING HUB
Python IDE
Sometimes we also need some help with the language code and features
to write our program correctly. A professional programmer may need many
more tools to develop, test, and organize his/her programs.
https://www.python.org/downloads/windows/
2.Find a stable Python 3 release. This tutorial was
tested with Python version 3.10.10.
3.Click the appropriate link for your system to
download the executable file: Windows installer
(64-bit) or Windows installer (32-bit).
VKS-LEARNING HUB
if you’re just getting started with Python and you want to install it with default
features as described in the dialog, then click Install Now and go to
Step 4 - Verify the Python Installation. To install other optional and advanced
features, click Customize installation and continue.
The Optional Features include common tools and resources for Python and you
can install all of them, even if you don’t plan to use them.
VKS-LEARNING HUB
Interactive mode
• When commands are entered directly in IDLE from the keyboard,
the Interpreter/IDLE is said to be in interactive mode.
• In this mode it prompts for a command with the prompt.
• Let us try our first command in Python in the interactive mode.
• Type print ("Hello World") at the prompt, and press Enter.
• This command is to display the message Hello World on the
screen. When you press Enter, the interpreter interprets this
command and gets it executed. This is shown in the following
figure:
VKS-LEARNING HUB
>>> is the Python prompt. If something is typed and it is syntactically correct, then
Python will display some output on the screen, otherwise it will display an error
message. Some examples are given below:
So, now we understand that we can give commands at the primary prompt and see the
result immediately.
print() is a function which is used to display the specified content on the screen. The
content, called argument(s), is/are specified within the parentheses.
Data types In Python, each value is an object and has a data type associated with
it. Data type of a value refers to the kind of value it is. Following are the built-in
fundamental data types in Python:
>>> 10
Displays 10 on the screen
In Python 10 is int (integer– (numbers without any fractional part).
We can use type() to check the data type.
>>> type(10)
Displays <class ‘int'> on the screen.
>>> 25.6
Displays 25.6 on the screen
In Python 25.6 is float (floating point – a number with at least one digit after the decimal
point).
We can use type() to check the data type.
>>> type(25.6)
Displays <class 'float'> on the screen.
>>> 'FAIPS'
Displays 'FAIPS' on the screen.
>>> "FAIPS"
Displays 'FAIPS' on the screen.
VKS-LEARNING HUB
In Python 'FAIPS' / "FAIPS" is str (string – sequence characters enclosed within '/"). We
can use type() to check the data type.
>>> type( “FAIPS” )
Displays <class 'str'> on the screen.
A string has to be enclosed within ' or ". But represent a string by starting with ' and
ending " or viceversa will be flagged as syntax error.
>>> 'FAIPS"
Will display following error message on the screen:
>>> "FAIPS'
>>> 5+2j
In Python (5+2j) is complex (complex number: 5 is the real part and 2j is the
imaginary part).
We can use type() to check the data type.
>>> type(5+2j)
Displays <class 'complex'> on the screen. Data type complex is not in the
syllabus.
VKS-LEARNING HUB
Arithmetic Operators
Operator Operands Result Remark
Power >>> 2**3 8 int ** int is int (finds base raised to the exponent)
** >>> 2.5**3.0 15.625 float ** float is float (finds base raised to the
>>> 4**3.0 64.0 exponent)
>>> (-2)**5 -32 int ** float is float (finds base raised to the exponent)
int ** int is int (finds base raised to the exponent)
VKS-LEARNING HUB
For example, in the expression -2*3+4-5, the first minus sign is a unary
minus and the second minus sign is a binary minus.
The multiplication and addition operators in the above expression are
binary operators.
VKS-LEARNING HUB
12 + 3 * 4 – 6 / 2 (12 + 3) * 4 – 6 / 2
= 12 + 12 – 6 / 2 = 15 * 4 – 6 / 2
= 12 + 12 – 3.0 = 60 – 6 / 2
= 24 – 3.0 = 60 – 3.0
= 21.0 = 57.0
12 + (3 ** 4 – 6) / 2 12 *( 3 % 4)// 2 + 6
= 12 + (81 – 6) / 2 = 12*3//2 + 6
= 12 + 75 /2 = 36//2 + 6
= 12 + 37.5 = 18 + 6
= 49.5 = 24
VKS-LEARNING HUB
>>> m=val
What happens when it is executed? Python does not create another
object. It simply creates a new symbolic name or reference, m, which
points to the same object that Val points to.
VKS-LEARNING HUB
>>> m = 500
Now Python creates a new integer object with the value 400, and m becomes a
reference to it.
Val 400
500 m
400
500 m
Object Identity
In Python, every object that is created is given a number that uniquely
identifies it. It is guaranteed that no two objects will have the same
identifier during any period in which their lifetimes overlap. Once an
object’s reference count drops to zero and it is garbage collected, as
happened to the 400 object above, then its identifying number becomes
available and may be used again.
The built-in Python function id() returns an object’s integer
identifier. It can be referred as memory location number
which the variable is pointing to.
VKS-LEARNING HUB
Python Comments
Comments are non executable lines which are ignored by Interpreter
Comments are used for documentation or explanation
Token
Smallest Individual unit in a program is known as a Token or a
Lexical unit.
Building block of a program is called a token.
It is also called program element.
Tokens of a Python can be classified as
• Keyword
• Identifiers (Names)
• Literals
• Operators
• Delimiters
VKS-LEARNING HUB
Keywords
• A keyword is a word having special meaning reserved by the
programming language.
We cannot use a keyword as variable name, function name or any other identifier.
In Python, keywords are case sensitive.
All the keywords except True, False and None are in lowercase and they must be
written as it is. The list of all the keywords are given below.
Keyword can not be redefined
VKS-LEARNING HUB
Python Identifiers
Identifier
Identifier is a component of a script which is identified by Python.
There are two broad categories of identifiers: Built-in and User Defined
1. Built-in: It is name of built-in functions or built-in objects. Some built-in
functions and objects can be used directly. But built-in functions and
objects needs appropriate module to be imported. We will discuss module
later.
Literals(Constant)
Literal: A literal is a program element whose value remains same
(constant) through the execution of the Python script.
Literals are the Values which does not change
Examples of different types of literals are given below:
Data Type Examples
Int (integer) -4, -3, -2, -1, 0, 1, 2, 3, 4, …
Float( floating point) -3.7, -1.0, -0/8, 0, 0.3, 1.8, 2.0, 3.9, ……
complex 4+2j, 0-5j, 4+7j
Bool(Boolean) False(0), True(1) both are keywords
Str(string) 'AMIT', '2.65', '***', 'GH-14/783, Paschim
Vihar', "RUPA", "1995", "$$$", "49 South
St, PO Box-9951"
VKS-LEARNING HUB
Operators
• Unary Operator (+, -, ~, not)
• Binary Operator
– Arithmetic Operator (+, -, *, /, %, **, //)
– Relational Operator (<, >, <=, >=, ==, !=)
– Logical Operator (and, or, not)
– Bitwise Operator (&, ^, |, <<, >>)
– Assignment Operator (=, +=, -=, *=, /=, %=,**=, //=)
– Identity Operator (is, not is)
– Membership Operator (in, not in)
VKS-LEARNING HUB
% Modulo Operator
b=q+r
= b*q+r
=b*(a//b)+r
a-(a//b)*b
or Division always take smallest int value
oward negative infinity)
od = n - math.floor(n/base) * base
VKS-LEARNING HUB
+= (Addition assignment): This operator is used to add the value on the right to the
variable on the left and then update the value of the variable.
x = 10 # x is assigned the value 10
x += 5 # x is updated to x + 5,
print(x) # 15
-= (Subtraction assignment): This operator is used to subtract the right side value from
the left side variable and then update the value of the variable.
x -= 3 # x is updated to x - 3,
print(x) # 12
*= (Multiplication assignment): This operator is used to multiply the value on the right
with the variable on the left and then update the value of the variable.
x *= 2 # x is updated to x * 2,
print(x)# 24
**= (Exponentiation assignment): This operator is used to raise the variable on the left
to the power of the value on the right and then update the value of the variable.
x **= 2 # x is updated to x ** 2,
print(x) # The output will be 576
VKS-LEARNING HUB
/= (Division assignment): This operator is used to divide the variable on the left
by the value on the right and then update the value of the variable.
x = 50 # x is assigned the value 50
x /= 2 # x is updated to x / 2, which is 25
//= (Floor division assignment): This operator performs floor division on the left
side variable by the right side value and then updates the value of the variable.
x //= 3 # x is updated to x // 3, which is 8
x %= 5 # x is updated to x % 5, which is 3
VKS-LEARNING HUB
Delimiters
Delimiter: are special symbol(s) that perform three special roles in
Python: grouping, punctuation and assignment.
List of Python delimiters are given below:
()[]{} used for grouping (More about grouping later)
. , : ; punctuation
= += -= *= /= //= %= **= assignment (also used as shorthand operator)
() uses with function name but not for grouping
. used in a float literal, calling function from a module, calling function of
an object
, used to assign multiple values to multiple variable in a single statement
: used in if statement, loops, function header ...
VKS-LEARNING HUB
expressions
• An expressions is any legal combination of
symbols(operators) that represent a value
• 15
• 2.9
• A=a+b
• A+4
• D>5
• F=(3+5)/2
VKS-LEARNING HUB
VKS-LEARNING HUB
Python Statement
• Instructions that a Python interpreter can execute are called statements.
• For example, a = 2 is an assignment statement
• if statement, for statement, while statement etc. are other kinds of statements.
Multi-line statement
In Python, end of a statement is marked by a newline character. But we can make a
statement extend over multiple lines with the line continuation character (\).
For example:
a=1+2+3+\
4+5+6+\
7+8+9
Multiple Statement in Single Line
We could also put multiple statements in a single line using semicolons, as
follows
a = 1; b = 2; c = 3
VKS-LEARNING HUB
Python Indentation
• Most of the programming languages like C, C++, Java use
braces { } to define a block of code. Python uses
indentation.
• A code block (body of a function, loop, class etc.) starts
with indentation and ends with the first unindented line.
• The amount of indentation is up to you, but it must be
consistent throughout that block.
• Generally four whitespaces are used for indentation and
is preferred over tabs.
VKS-LEARNING HUB
Python Run-time
Error Syntactically correct Python line of code performs illegal operation during execution of
a program (runtime).
Illegal operation is performed when the Python code encounters an unexpected data during
the runtime and program halts
Examples:
Division by zero (0),
Square root of a negative number,
Log of zero (0) or negative number