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

VKS Python

Uploaded by

Vinod Srivastava
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

VKS Python

Uploaded by

Vinod Srivastava
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 57

VKS-LEARNING HUB

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

What can I do with Python…?


• Graphical User Interface Programming
• System programming
• Internet Scripting
• Component Integration
• Database Programming
• Gaming, Images, XML , Robot and more… ..
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

Compiling and interpreting


• Many languages require you to compile (translate) your program into a form
that the machine understands.
compile execute
source code byte code output
Hello.java Hello.class

• Python is instead directly interpreted into machine instructions.


interpret
source code output
Hello.py

5
VKS-LEARNING HUB

Python IDE

A computer understands only the machine language.


It does not understand programs written in any other
programming language like Python. Therefore, for
program execution, each instruction of the program
(written in Python) is first interpreted into machine
language by a software called the Python
Interpreter, and then it is executed by the computer.
So, if we wish to run a Python program on a
computer, we should first install Python Interpreter
on it.
VKS-LEARNING HUB

 The process of programming starts with the problem to be solved. When


there is a problem to be solved, we have to think computationally to
formulate the problem well, find its well-defined solution, write the program
to represent the solution, and execute the program. We need a text editor
(Like Notepad, Notepad2, Notepad++, etc) to type the program.

 After the program is typed, we need an Interpreter to interpret and


execute/run the program.
 Sometimes the program has some bugs (errors in a program are called
bugs), and we need to debug the program. If these bugs are difficult to
identify, we need some debugging tools on the computer to help us.

 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.

 A software that provides all these tools as a bundle is called an IDE


(Integrated Development Environment). When we install Python on our
computer, we actually install the Python IDE. Python IDE is called IDLE
(Integrated Development and Learning Environment).
VKS-LEARNING HUB

Step 1 — Downloading the Python Installer


1.Go to the official
Python download page for Windows.

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

Step 2 — Running the Executable Installer


 After the installer is downloaded, double-click the .exe file, for example python-
3.10.10-amd64.exe, to run the Python installer.
 Select the Install launcher for all users checkbox, which enables all users of the
computer to access the Python launcher application.
 Select the Add python.exe to PATH checkbox, which enables users to launch
Python from the command line.
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

Select some or all of the


following options:
1.Click Next.
2.The Advanced Options dialog
displays.
3. Click Install to start the installation.

After the installation is complete,


a Setup was successful message displays.
VKS-LEARNING HUB

When you start IDLE, computer opens the IDLE window


(Called the Python Shell) as shown below

It shows you some information and a prompt (>>>) followed by


a blinking cursor. It indicates that IDLE is now ready to take
Python commands from the user.

IDLE can be used in two modes: Interactive mode and Script


mode.
VKS-LEARNING HUB

IDLE – Development Environment


• IDLE helps you
program in Python
by:
– color-coding your
program code
– debugging
– auto-indent
– interactive shell
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.

Python is a case-sensitive language. It means that Python differentiates between


capital and small alphabets. For example, Print (P capital) and print (p small) are two
different words for Python. Where print is a valid command in Python, Print is just a
word and not a command.
VKS-LEARNING HUB

This can be observed by typing the command Print ("Hello World").


Python will not be able to interpret it and will show an error.
This is shown in the following figure:

In the print command, if the argument is put within


quotation marks (single '' or double ""), then the argument
is displayed as it is. We have already seen this in the
above examples.
If the argument is not placed within quotation marks,
then the interpreter tries to evaluate the argument and
displays the result.
This feature is used to perform calculations.
VKS-LEARNING HUB

Using IDLE as a calculator:


To evaluate an arithmetic expression, it is not mandatory
to use print() function.
If we type an arithmetic expression at the prompt and
press Enter,
the interpreter automatically evaluates it and shows the
result.
This is shown in the following figure:
VKS-LEARNING HUB

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:

Fundamental / Primitive / Built-in data types of Python and Literals (constants):


Data Type Literals(Constant)

int (Integer) 0,1,2,3,20,3000,4567,-4,-45,-10000,…..


float(floating point) 2.3,4.5.66.78,989.567,-56.89,0,9,-
0,67……..
Str(string) ‘AMIT’ , “Rohini”, “****”, ‘2.56’,
‘Kuwait’, “FAIPS-DPS”,
“No#1”, “49 South St, PO Box-9951”

bool True, False more will be discussed later


along logical expression
Complex(complex) 5+6j, 2-5j, ….
VKS-LEARNING HUB

>>> 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:

SyntaxError: EOL while scanning string literal

>>> "FAIPS'

Will display following error message on the screen:

SyntaxError: EOL while scanning string literal


VKS-LEARNING HUB

>>> 5+2j

Displays (5+2j) on the screen.

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

+ >>> 10+20 30 int + int is int (adds two numbers)


>>> 2.5+3.8 6.3 float + float is float (adds two numbers)
>>> 3.6+8 11.6 float + int is float (adds two numbers)
>>> 'MAN'+'GO MANGO Joins two strings (concatenation)
- >>> 35-17 18 int - int is int (subtracts two numbers)
>>> 7.2-3.8 3.4 float - float is float (subtracts two
>>> 8-4.2 3.8 numbersint - float is float (subtracts two
numbers)
* >>> 5*4 20 int * int is int (multiplies two numbers)
>>> 3.5*4.5 15.75 float * float is float (multiplies two
>>> 4*2.5 10.0 numbers)
int * float is float (multiplies two numbers)
Real >>> 12/4 3.0 int / int is float (divides two numbers)
Division >>> 2/3 0.6667 int / int is float (divides two numbers)
/ >>> 12.5/2.5 5.0 float / float is float (divides two numbers)
>>> 8.3/12.6 0.6588 float / float is float (divides two numbers)
>>> 10/3.5 2.857 int / float is float (divides two numbers)
VKS-LEARNING HUB

Operator Operands Result Remark


Floor >>> 12//4 3 int // int is int (divides two numbers)
Division >>> 2//3 0 int // int is int (divides two numbers)
// >>> 12.5//2.5 5.0 float // float is float (divides two numbers)
>>> 8.3//12.6 0.0 float // float is float (divides two numbers)
Answer Is >>> 10.5//4 2.0 float // int is float (divides two numbers)
the quotient >>> 10//3.5 2.0 int // float is float (divides two numbers)

% >>> 12%4 0 int % int is int (finds remainder of two numbers)


Modulo >>> 2%3 2 int % int is int (finds remainder of two numbers)
>>> 12.5%2.5 0.0 float % float is float (finds remainder of two numbers)
>>> 8.3%12.6 8.3 float % float is float (finds remainder of two numbers)
>>> 10.5%4 2.5 float % int is float (finds remainder of two numbers)
>>> 10%3.5 3.0 int % float is float (finds remainder of two numbers)

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

Unary and Binary operators: An operator can be termed as unary or


binary depending upon the number of operands it takes.
A unary operator takes only one operand, and a binary operator takes two
operands.
Examples of Unary Operators : Unary -, unary + , not
Examples of Binary Operators: Binary +, -, /, +=, *=, >=, !=, and, or

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

Operator precedence If an expression contains only one


operator, then it can be evaluated very easily. But, if an
expression contains more than one operators, then to
evaluate it we have to be careful about the sequence in
which the operations have to be done.

Operator precedence determines which operators are


applied before others when an expression contains more
than one operators.
Precedence of Arithmetic operators in Python
Highest ** (works from left to right)
Intermediate *, /, //, % (works from left to right)
Lowest +, - (works from left to right)
() parenthesis is used to override the precedence of an
operator
VKS-LEARNING HUB

Following is the order of precedence of operators:


(**) > (unary +, unary -) > (*, /, //, %) > (binary +, binary -)

Higher precedence operators are applied before the lower


precedence operators.
When an expression contains operators which have the
same precedence (like * and /), then whichever operator
comes first is evaluated first.

Parentheses can be used to override the order of


precedence and force some part of the expression to be
evaluated before others
VKS-LEARNING HUB

Use of operator precedence in Arithmetic Expression Evaluation

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

Evaluate the following expressions


(i)(2 + 3) ** 3 - 6 / 2
(ii)(2 + 3) * 5//4+(4 + 6)/ 2
(iii)12 + (3 * 4 – 6) / 3
(iv)12 + (3 * *4 – 6)// 2
(v)12 * 3 % 5 + 2 * 6//4
(vi)12 % 5 *3 +(2*6)//4
VKS-LEARNING HUB

Variable (identifier) in Python

 A variable is name given to a memory location to store value


in the computer’s main storage (in RAM).
 It is a name used in the program that represents data (value).
 The program can always access the current value of the
variable by referring to its name.
 In Python variable is created when it is assigned a value.
=operator is used to assign value to variable
Roll=1
price=100.5
 It means, in Python to create a variable, a value must be
assigned to it. In Python variable is an Object.
 The contents of a variable may change during the programs
execution.
VKS-LEARNING HUB

RULES FOR DECLARING VARIABLE


 Variable name should start with either an alphabet (letter) or
an underscore. Variable name starting with _ (underscore)
has special meaning
- Names like myClass, var_1 and print_this_to_screen,
all are valid
 An identifier cannot start with a digit.
- 1variable is invalid, but variable1 is perfectly fine.
 Variable name may contain more than one character.
Second characters onwards we may use alphabet or digit or
underscore.
 Keywords cannot be used as identifiers.
 We cannot use special symbols like !, @, #, $, % etc. in our
identifier.
 A variable name in python is case sensitive. Uppercase and
lowercase letters are distinct
VKS-LEARNING HUB

Examples of correct variable names are given below:


marks,m1,m2,Father_Name,Max_Score,sub1code,ans,
Roll,Val,Input_Scr, For, CSMarks, false, true, CLASS,…
Examples of correct variable names are
given below:
• Data-rec
• 48LIC
• break
• My.file
• First name
• while
VKS-LEARNING HUB

When using assignment operator, there should be one and


only one variable on the left hand side of assignment
operator.
This variable is called the Left value or the L-value.
There can be any valid expression on the right hand side of
assignment operator.
This expression is called the Right value or R-value.

The statement: L-value = R-value

is called the assignment statement. When the interpreter


encounters an assignment statement, it evaluates the right
hand side expression (R-value) and assigns its value to the
left hand side variable (L-value).
VKS-LEARNING HUB

Rule for creating variable


Single Variable declaration statement
VarName=Value
21 is an int constant. By assigning 21 to variable
Roll=21 the Roll
Roll becomes an int variable.
Name=“RUPA JAIN”
'RUPA JAIN' is a str constant. By assigning
Marks=89.5 'RUPA JAIN' to the variable Name
Name becomes a str variable
89.5 is a float constant. By assigning 89.5 to the
variable Marks
Marks becomes a float variable.

Multiple Variable declaration in single statement

VarName1,VarName2,VarName3 = Value1, Value2, Value3


Roll, Name,Marks =1, “RUPA JAIN” ,89.
VKS-LEARNING HUB

Multiple Variable declaration in single statement


VarName1=VarName2=VarName3 = Value
Jan=Mar=May=31
Here multiple variable are assigning same value
VKS-LEARNING HUB

A Python variable is a symbolic name that is a reference or


pointer to an object. Once an object is assigned to a variable, you
can refer to the object by that name. But the data itself is still
contained within the object.
>>> Val=400
This assignment creates an integer object with the value 400 and
assigns the variable Val to point to that object.
Val 400 m

>>> 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

>>> Val = “AAA”


Now Python creates a string object with the value “AAA" and
makes Val reference that.
Val AAA

400

500 m

There is no longer any reference to the integer object 400. It is orphaned,


and there is no way to access it. 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.
VKS-LEARNING HUB

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

Single Line Comment:


• In Python, we use the hash (#) symbol to start writing a
comment.
#This is a comment
#print out Hello
print('Hello')
Multi-line comments
• Another way of doing this is to use triple quotes, either ''' or """.
"""This is also a
perfect example of
multi-line comments""“
print(“Hello”)
VKS-LEARNING HUB

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 is the name given to entities like class,


functions, variables etc. in Python. It helps differentiating
one entity from another. Identifiers can be redefined
Rules for writing identifiers
• Identifiers can be a combination of letters in lowercase (a to z) or
uppercase (A to Z) or digits (0 to 9) or an underscore (_). Names
like myClass, var_1 and print_this_to_screen, all are valid example.
• An identifier cannot start with a digit. 1variable is invalid,
but variable1 is perfectly fine.
• Keywords cannot be used as identifiers.
• We cannot use special symbols like !, @, #, $, % etc. in our identifier.
• Identifier can be of any length.
VKS-LEARNING HUB

Valid Identifiers Invalid Identifiers


• Myfile • Data-rec
• School_46 • 48LIC
• _don_ • break
• _fall_down • My.file
• Up_and_down_22 • First name
• BREAK • while
• WHILE
VKS-LEARNING HUB

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.

Built-in identifier can be redefined (built-in identifier can be used as variable


name).
>>> pow(36,0.5)
Displays 6.0 on the screen pow() function (built-in identifier) is as an
alternative to ** operator
>>> pow=25 #using pow as a variable name >
>> pow Displays 25 on the screen
>>> pow(4,2) Displays an error message on the screen since pow is
used as a variable name. Hence it cannot calculate 4**2.
Therefore, it is a bad practice to use a built-in identifier as a variable
name.
VKS-LEARNING HUB

Some more built in identifier we have used so far


print(),type(),input(),int(),float(),str(),id(),pow()…. And many
more we learn in near future with our progress in learning
python

2. User-defined: Identifiers created by the programmer like


variable names, user-defined function names, and class
names. User-defined identifiers can only be used after they
have created or declared.
Till now variables are the only user-defined identifiers, being
created in a Python script. User defined function and class will
be discussed later.
VKS-LEARNING HUB

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

-7%3 7%-3 -7%-3


a=-7 a=7 a=-7
b=3 b=-3 b=-3
r= -7-(-7//3)*3 r=a-(a//b)*b r=a-(a//b)*b
r=-7-(-3)*3 r=7-(7//-3)*-3 r=-7-(-7//-3)*-3
r=-7+9 =2 r=7-(-3)*-3 -7+6
r=7-9 =-1
r=-2
VKS-LEARNING HUB
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

%= (Modulus assignment): This operator calculates the modulus of the left


side variable divided by the right side value and then updates the value of the
variable.

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 Syntax error


 Syntax error arises when Python is unable to understand a line of code due to the
grammatical error in Python.
 Line of code with syntax error will never be executed successfully.
Example:
 Most syntax errors are typos,
 incorrect indentation,
 incorrect arguments when calling a function.

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

Python Logic error


 Syntactically correct Python statement giving unpredictable results (outputs) and may crash
your program due to error in the design or in the logic in the code.
Examples:
 Missing parenthesis when parenthesis is required,
 Counter/Accumulator initialized incorrectly

You might also like