Unit 1 Fundamentals of Python Programming
Unit 1 Fundamentals of Python Programming
Python
SCHOOL OF COMPUTER ENGINEERING AND TECHNOLOGY
• Program flow control: Conditional statement, Looping and Iteration, Range Functions
3
Introduction to Python
Text Books:
Reference Books:
4
Introduction to Python
Why Programming ??
• We live in a digital society where everyone uses a computer or a mobile phone or
most of the times both.
• It's one thing to know how to use the apps/programs on such digital devices and it's
totally another to know how the logic behind them works.
• In this digital age the knowledge of programming is essential in order to bring
innovation and change.
• To create value with your own ideas you need to know how to code.
• Programming has become basic literacy for the 21st century.
What is Python??
• Python is a widely used high-level, general-purpose, interpreted, dynamic
programming language.
• Python is founded by Guido van Rossum is a Dutch programmer who is best known as
the author of the Python programming language. 5
Introduction
Python is Interpreted: Python is processed at runtime by the interpreter. You do not need
to compile your program before executing it.
Python is Interactive: You can actually sit at a Python prompt and interact with the
interpreter directly to write your programs.
Python is Object-Oriented: Python supports Object-Oriented style or technique of
programming that encapsulates code within objects.
Python is a Beginner's Language: Python is a great language for the beginner level
programmers and supports the development of a wide range of applications from simple
text processing to WWW browsers to games.
09/01/2022 6
About Python Programming
•Free and open-source - You can freely use and distribute Python, even for commercial use.
•Easy to learn - Python has a very simple and elegant syntax. It's much easier to read and
write Python programs compared to other languages like C++, Java, C#.
•Portable - You can move Python programs from one platform to another, and run it without
any changes.
09/01/2022 8
Python Applications
• Web Development. ...
• Game Development. ...
• Scientific and Numeric Applications. ...
• Artificial Intelligence and Machine Learning. ...
• Software Development. ...
• Enterprise-level/Business Applications. ...
• Education programs and training courses. ...
• Language Development.
09/01/2022 9
09/01/2022 10
09/01/2022 11
09/01/2022 12
09/01/2022 13
09/01/2022 14
09/01/2022 15
Question ??
09/01/2022 16
09/01/2022 17
Input Output Processing
1.take input from a user and system In Python
2.Accept an integer, float, character, and string input from a user.
3.Convert the user input to a different data type.
4.output formatting.
09/01/2022 20
2. Click on the downloads tab and choose the operating system and python version.
So, here I am downloading python version 3.8.4 for Windows operating system
09/01/2022 21
On command Line
>>> print("Hello World")
Hello World
>>> 2+3
5
>>> 5-2
3
>>> 3*5
15
>>> 45/5
9.0
>>> 2**4
16
09/01/2022 22
Installing IDE: Anaconda
Steps:
1.Visit Anaconda.com/downloads
2.Select Windows
3.Download the .exe installer
4.Open and run the .exe installer
5.Open the Anaconda Prompt and run some Python code
09/01/2022 23
09/01/2022 24
Displaying Output with the Print
a=int(input("enter value of a"))
print("Hello World") b=int(input("enter value of a"))
a=10;b=20; c=a+b
c=a+b print("addition of a with b is",c)
print(c)
a=10;b=20; a=int(input("enter value of a"))
c=a+b b=int(input("enter value of a"))
print("Addition of two number is ",c) c=a*b
a=10; print("multiplication of a with b is",c))
print(a)
a="MITWPU"
print(a)
a=true
print(a)
a=True
print(a)
09/01/2022 25
09/01/2022 26
Comments in Python
• Comments in any programming language are used to increase the readability of the code
• by reading a comment you can understand the purpose of code much faster then by just
going through the actual code.
In this Python program we are seeing three types of comments. Single line comment, multi-line
comment and the comment that is starting in the same line after the code.
Single line comment
# This is a single line comment in Python
Inline comment:
print("Hello World") # This line prints "Hello World“
multi-line comment
""" We are writing a simple program here
First print statement.
This is a multiple line comment"""
09/01/2022 27
Comments in Python
Multiple line comment
In python we use # special character to start the comment.
E.G
print("Hello World") # printing 1st statement in python
a=int(input("enter value of a"))
b=int(input("enter value of a"))
c=a*b
print("multiplication of a with b is",c) # addition is stored in variable c
09/01/2022 28
Variables
09/01/2022 29
Variables
09/01/2022 30
Variables
Variables in Python
You can consider a variable to be a temporary storage space where you can keep changing values
So, here this cart acts like a variable, where the values stored in it keep on
changing.
09/01/2022 31
Variables Types
Assigning values to variables
• Python variables do not need explicit declaration to reserve memory space.
• The declaration happens automatically when you assign a value to a variable.
• The equal sign (=) is used to assign values to variables.
b = 9223372036854775807
print(b)
32
Variables Types
Multiple Assignment
Python allows you to assign a single value to several variables simultaneously
b = 9223372036854775807
print(type(b))
pi = 3.14
print(type(pi))
c = 'A'
print(type(c))
q = True
09/01/2022 print(type(q)) 34
keywords
import keyword
print(keyword.kwlist)
09/01/2022 35
keywords
09/01/2022 36
keywords
a=10; True=20;
c=a+True
print(c)
a=10; true=20;
c=a+true
print(c)
09/01/2022 37
Reading Input from the Keyboard
• Python user input from the keyboard can be read using the input() built-in function.
• The input from the user is read as a string and can be assigned to a variable.
• After entering the value from the keyboard, we have to press the “Enter” button.
• Then the input() function reads the value entered by the user
09/01/2022 38
Python Identifiers
An identifier is a name given to entities like class, functions, variables, etc.
It helps to differentiate one entity from another.
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)
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 a valid name.
• Keywords cannot be used as identifiers.
e.g. global=1
SyntaxError: invalid syntax
• We cannot use special symbols like !, @, #, $, % etc. in our identifier
a@=0
• An identifier can be of any length. 39
Python Identifiers
• Python is a case-sensitive language. This means, Variable and variable are not the same.
• Always give the identifiers a name that makes sense.
• While c = 10 is a valid name, writing count = 10 would make more sense, and it would be easier to
figure out what it represents when you look at your code after a long gap.
• Multiple words can be separated using an underscore, like this_is_a_long_variable.
40
Python Literals
Literal is a raw data given in a variable or constant. In Python, there are various types of literals
1.Numeric Literals
• Numeric Literals are immutable (unchangeable).
• Numeric literals can belong to 3 different numerical types: Integer, Float, and Complex.
2.String literals
• A string literal is a sequence of characters surrounded by quotes.
• We can use both single, double, or triple quotes for a string.
• And, a character literal is a single character surrounded by single or double quotes
43
Operators
09/01/2022 44
Arithmetic Operators
Arithmetic Operator Operator Name Description Example
I=40, J=20
+ Addition Performs addition >>>I+ J
>>>60
I=40, J=20
– Subtraction Performs subtraction >>>I – J
>>>20
I=40, J=20
* Multiplication Performs multiplication >>>I * J
>>> 800
I=30, J=20
/ Division Performs division >>>I /J
>>> 2.5
I=40, J=20
Returns the remainder after
% Modulus >>>I /J
the division
>>> 0
I=4, J=20
Performs exponential (power)
** Exponent >>>I /J
calculation
>>> 204
09/01/2022 45
Relational Operators in Python
Operator Operator Name Description Example
If values of two operands are I = 20, J = 20
== Equal to
equal, then it returns true. (I == J) is True
If values of two operands are I = 20, J = 20
!= Not Equal to
not equal, then it returns true. (I == J) is False
If the value of the left operand
is less than the value of the I = 40, J = 20
< Less than
right operand, then it returns (I < J) is False
true.
If the value of the left operand
is greater than the value of the I= 40, J = 20
> Greater than
right operand, then it returns (I > J) is True
true.
If the value of the left operand
is less than or equal to the I = 40, J = 20
<= Less than or equal to
value of the right operand, then (I <= J) is False
it returns true.
If the value of the left operand
is greater than or equal to the I = 40, J = 20
>= Greater than or equal to
value of the right operand, then (I >= J) is True
it returns true.
If values of two operands are
I=40, J = 20
<> Not equal to (similar to !=) not equal, then the condition
(I <> J) is True.
becomes true.
09/01/2022 46
Assignment Operators in Python
Operator Operator Name Description Example
It assigns a value from the right-
I = 40
= Assignment side operand to the left-side
It assigns 40 to I
operand.
It performs addition, and then the
I+=J
+= Add then assign result is assigned to the left-hand
that means I = I + J
operand.
It performs subtraction, and then
I-=J
-= Subtract then assign the result is assigned to the left-
that means I = I – J
hand operand.
It performs multiplication, and
I*=J
*= Multiply the assign then the result is assigned to the
that means I = I * J
left-hand operand.
It performs division, and then the
I/=J
/= Divide then assign result is assigned to the left-hand
that means I = I / J
operand.
09/01/2022 48
Basic Data Types
One way to categorize these basic data types is in one of four groups:
• Numeric:
int, float and the less frequently encountered complex
• Sequence: (string)
09/01/2022 49
Basic Data Types
09/01/2022 50
Basic Data Types
09/01/2022 51
Python Data Type: Strings
Strings: Strings in Python are used to store textual information
A string is a sequence of characters. E.G. ‘MITWPU’ “Pune”
A character is simply a symbol. For example, the English language has 26 characters.
Strings in Python are identified as a contiguous set of characters represented in the quotation marks.
Python allows either pair of single or double quotes.
str[22] str1="python"
IndexError: string index out of range print(str1)
# index must be an integer
str[2.2] print(str1)
...
TypeError: string indices must be integers #del str
print(str1)
str="python"
print(str) We cannot delete or remove characters from a string
str="python"
str[1]=q
print(str)
"MITwpu".upper()
"MITwpu".lower()
09/01/2022 57
Control-Flow: For Loop
For Loop:
for number in 1,2,3,4,5:
print("The current number is ",number)
range is a function that returns a series of numbers under an iterable form, thus it can be used in for loops:
for i in range(10):
print(i)
for i in range(2,10,2):
print(i)
for i in numbers_list:
square = i*i
print("The square of", i, "is", square)
09/01/2022 59
Control-Flow: For Loop
# Nested Loop
persons = [ "John", "Marissa", "Pete", "Dayton" ]
restaurants = [ "Japanese", "American", "Mexican", "French" ]
09/01/2022 60
Control-Flow: For Loop
# Nested Loop
number_of_passengers=5
number_of_baggage=2
security_check=True
if(security_check==True):
else:
09/01/2022 61
Control-Flow: For Loop
break statement
• while loops, for loops can also be prematurely terminated using the break statement.
• The break statement will immediately terminate the execution of the loop and transfer the control of the program
to the end of the loop.
number_list = [2,3,4,5,6,7,8]
for i in number_list:
print(i)
if i == 5:
break
continue Statement : the continue statement can also be used in Python for loops to terminate the ongoing iteration
and transfer the control to the beginning of the loop to continue the next iteration.
number_list = [2,3,4,5,6,7,8]
for i in number_list:
if i == 5:
continue
print(i)
09/01/2022 62
Control-Flow: For Loop
for passenger in "FC", "C", "FA", "SP", "A":
if(passenger=="FC" or passenger=="FA"):
print("No check required")
continue
if(passenger=="SP"):
print("Declare emergency in the airport")
break
if(passenger=="A" or passenger=="C"):
print("Proceed with normal security check")
09/01/2022 63
Control-Flow: While Loop
• While loop statements in Python are used to repeatedly execute a certain statement as
long as the condition provided in the while loop statement stays true.
• While loops let the program control to iterate over a block of code.
while test_expression:
body of while
09/01/2022 64
Control-Flow: While Loop
a=0
while a<10: a=1
a = a+1 while a <5:
print(a) a += 1
a=1 if a == 3:
break
while a<5: print(a)
print("condition is true")
a=a+1 a=1
else:
print("condition is false now") while a <5:
a += 1
a = 10 if a == 3:
while True: continue
a = a-1 print(a)
print(a)
if a<7:
break
print('Done.')
09/01/2022 65
66