Introduction
•Python programming language
was developed by Guido Van
Rossum in 1991.
•Python and was named after the
BBC TV show Monty Python’s
Flying Circus.
Features of Python
• Easy - Python is very easy to learn and understand.
• Interpreted - It is interpreted(executed) line by line. This makes it
easy to test and debug.
• Object-Oriented - Python is a General Purpose object-oriented
programming language, which means that it can model real-world
entities.
• Free and Open Source - The language and its source code are
available to the public for free; there is no need to buy a costly
license.
• Portable/ Cross-Platform - Can run it on any platform like Windows,
Mac, Linux etc.
• GUI Programming - It can be used to develop a GUI (Graphical
User Interface). One way to do this is through Tkinter.
Applications of Python
• Build a website using Python
• Develop a game in Python
• Perform Computer Vision (Facilities like face-detection and color-
detection)
• Implement Machine Learning (Give a computer the ability to learn)
• Enable Robotics with Python
• Perform Web Scraping (Harvest data from websites)
• Perform Data Analysis using Python
• Automate a web browser
• Perform Scripting in Python
• Perform Scientific Computing using Python
• Build Artificial Intelligence
Installing Python
•Before start working in Python, it has to be installed
in our system.
•Default installation, Python IDLE (Integrated
Development and Learning Environment) is
available from [Link]
•Or Anaconda installation is available from
[Link] (After installing the anaconda
navigator, launch Spyder).
Installing Python IDLE
1) Download the Python 3.7.3 (32 bit/64 bit as per
your system configuration).
Select this checkbox
before clicking install
now
Installing Python IDLE
Installing Python IDLE
Click the close button
Installing Anaconda
1) Download the Anaconda installer.
2) RECOMMENDED
3) Double click the installer to launch.
4) Click Next.
5) Read the licensing terms and click “I Agree”.
6) Select an install for “Just Me” unless you’re installing for all
users (which requires Windows Administrator privileges)
and click Next.
7) Select a destination folder to install Anaconda and click
the Next button.
Installing Anaconda
Installing Anaconda
8) Choose whether to add Anaconda to your PATH environment variable. We
recommend not adding Anaconda to the PATH environment variable, since
this can interfere with other software. Instead, use Anaconda software by
opening Anaconda Navigator or the Anaconda Prompt from the Start Menu.
Installing Anaconda
9. Choose whether to register Anaconda as your default Python. Unless you plan on
installing and running multiple versions of Anaconda or multiple versions of Python,
accept the default and leave this box checked.
10. Click the Install button. If you want to watch the packages Anaconda is installing,
click Show Details.
11. Click the Next button.
Optional: To install PyCharm for Anaconda,
click on the link to
[Link]
Or to install Anaconda without PyCharm,
click the Next button.
Installing Anaconda
12. After a successful installation you will see the “Thanks for installing Anaconda” dialog
box.
13. If you wish to read more about [Link] and how to get started with Anaconda,
check the boxes “Anaconda Individual Edition Tutorial” and “Learn more about
Anaconda”. Click the Finish button.
Installing Anaconda
14. Verify your installation.
Windows: Click Start, search or select Spyder from the menu.
Working Modes in Python
• There are two working modes in Python:
a) Interactive mode (Immediate mode)
b) Script mode
Interactive mode Script mode
Can type only one command at a Can type multiple commands at a time.
time.
It will give output immediately It will give the complete output.
between the commands one at a
time.
Python’s
•Cannot interactive interpreter
save the commands.
is also called Python Shell.
Can save the commands with .py
extension
It is suitable for testing code. It is suitable for creating programs
Run a Code in Python IDLE
•A quick way to find your Python IDLE on Windows
is by clicking on the start menu. You should then
see the IDLE under “Recently added”.
Run a Code in Python IDLE
•Once you click on the Python IDLE, you’ll see the
Shell screen:
Python Prompt
Interactive
mode in
IDLE
Run a Code in Python IDLE
•Click on File and then select New File to get the
script mode (alternatively, you may use the
keyboard shortcut of Ctrl+N):
Run a Code in Python IDLE
•You would now see the following “untitled” box,
where you can type your Python code:
Run a Code in Python IDLE
•For example, type/copy the command below. This
command will print the famous expression of
“Hello World”
print ("Hello World")
Run a Code in Python IDLE
•Press F5 on your keyboard or go to Run->Run
Module. You will then get the following message
to save your code:
Run a Code in Python IDLE
•Choose a location where the Python file will be
saved on your computer. You’ll also need to type
a name for your file. For example, type “Test” for
your file name:
•Once you’re done, press Save, and you’ll then see
the “Hello World” expression printed on your
Python Shell: Output of Script
mode appears
in Interactive
Mode
Run a Code in Spyder
Click on Spyder and the editor opens.
Type the command
print("Hello World")
Script
Mode Interactive
Mode
Run a Code in Spyder
Click on the to run the file or press F5. It will be asked to save your
file. For example ([Link])
Run a Code in Spyder
You can see the output to the code
Interactive
Mode, Output
of Script mode
appears here
print() function in Python
print() function in python is used to print an output.
Interactive mode Script mode Output
The output of Script mode will
will be displayed in interactive
mode.
print() function
•A print() function without any value will prints a
blank line.
Output
print() function with multiple items
• A print() function automatically adds a space
between the items printed in a line.
Example:
Spaces can be seen added between the items in both
the outputs.
Multiple print() functions
• When multiple print() functions are there the outputs
of each print() is displayed in separate lines.
• Example:
• Output:
Output in separate
lines.
sep and end arguments of print()
function
• The print() function has two arguments sep and end.
• The sep argument specifies the separator characters.
By default, it is a space. Hence multiple items are
printed in a line it will add a space.
• The end parameter is used to append any string at
the end of the output of the print statement in Python.
By default, newline character (‘\n’) is appended to
the end of print() using end argument. Hence multiple
print() are printed in separate lines
sep argument of print() function
Example:
In the above examples the character given in the
sep argument is inserted between the items.
end argument of print() function
Example:
In this example the end
argument is given at first
print() statement. So the
output of the second print()
Output: is appended after the end
character ‘–’ given. Since
the end argument is not
given to second print(), the
third output is not
appended to the second.
end argument of print() function
Example:
In this example, Since the end
argument is not given to first
print(), the second output is
not appended to the first
Output: print(). the end argument is
given at second print()
statement. So the output of the
third print() is appended after
the end character ‘–’ given.
sep and end arguments together
in a print() function
Example:
Output:
To do
Consider the following code: (ii)
(iii)
Convert the above
statements to get the given
outputs: (iv)
(i)
Answers
(i)
(ii)
(iii)
(iv)
Character Set
•Character set is a set of valid characters that a
language can recognize. A character represents
any letter, digit, or any other symbol.
1) Letters
2) Digits
3) Special Symbols
4) Whitespaces etc.
Tokens / Lexical Unit
•The smallest individual unit in a program is known
as tokens or a lexical unit.
•Python has following tokens:
i. Keywords
ii. Identifiers
iii. Literals
iv. Operators
v. Punctuators
Keywords
•Keywords are the words that convey a special
meaning to the language compiler/interpreter.
•They are reserved by programming language and
must not be used as normal identifier names.
•To list the keywords in Python:
>>> import keyword
>>> [Link]
Identifiers
•Keywords are the words that convey a special
meaning to the language compiler/interpreter.
•They are reserved by programming language and
must not be used as normal identifier names.
•To list the keywords in Python:
>>> import keyword
>>> [Link]
Variables and Assignments
•Variables are named memory locations. It refers
to a value, that can be used during program
execution.
•To create a variable marks with value 70, we will
write as
marks = 70
Variable Assignment Operator Value
Variables and Assignments
Valid Assignment of a Variable
• a = 10
• b = 20
•c = a + b
Invalid Assignment of a Variable
• 70 = marks
• 10 = a
• 20 = b Error
•a + b = c
Variable Naming Rules
•Variables can contain alphabets (uppercase and
lowercase), numbers and the special character _
(underscore).
•The first character should be an alphabet or
underscore.
•It should not start with a number.
•No other special character is allowed other than
underscore.
To do
Which among the following is a valid variable?
If not give reason.
(i) First Name
(ii) _Check
(iii) 8data
(iv) [Link]
(v) age7
Answers
(i) First Name - Invalid, Space is not allowed
between the words (Valid ones - FirstName,
First_Name)
(ii) _Check - Valid
(iii) 8data - Invalid, Cannot start with a number
(iv) [Link] - Invalid, . (dot) is not allowed
(v) age7 - Valid
Multiple Assignments
i) Assign same value to (ii) Assign multiple values
multiple variables to multiple variables
a=b=c=10 a, b, c = 10, 20, 30
Here all the three It will assign the values
variables ‘a’, ‘b’ and order wise.
‘c’ have the value 10.
Python Program
1) WAP to store the value 10 and 15 in variables ‘a’
and ‘b’ respectively and print those values.
Program Code: Output
Here the value of ‘a’ and ‘b’ and constant. The
output will be the same as always.
Python Programs
2) Consider the previous question, add the values
of ‘a’ and ‘b’ and store it in another variable
‘sum’. Print the sum
Program Code: Output
Here the value of ‘a’ and ‘b’ and constant. The
output will be the same always.
input() function
•input() function is used to get the value from the
user interactively.
•Example:
a = input(“Enter the value”)
name = input(“Enter your name:”)
• The input() function always returns a value of
string (group of characters) type.
Python Program
1) WAP to accept your name and age from the
user and print the same.
Program Code:
Output:
Operators in Python
•Operators are used to perform some operations
on values and variables.
• The various operators in Python are:
a) Arithmetic/Mathematical Operators
b) Relational/Comparison Operators
c) Logical Operators
Arithmetic Operators in Python
• Arithmetic operators are used to performing
mathematical operations like addition, subtraction,
multiplication, and division etc.
• + Addition
• - Subtraction
• * Multiplication
• / Division Returns the output with decimal
• // Floor Division Returns the integer part only
• ** Exponent (raise to power)
• % Reminder/Modulus
Arithmetic Operators in Python
•Example
Output
Relational Operators in Python
•Comparison /Relational operators compares the
values. It either returns True or False according to
the condition.
• < Less than
• > Greater than
• <= Less than or equal to
• >= Greater than or equal to
• == Equal to
• != Not equal to
Relational Operators in Python
•Example
Output
Logical Operators in Python
•Logical operators are used to combine conditional
statements. It returns True or False according to the
condition.
• and (&) Logical AND Returns True if both
conditions are True
• or (|) Logical OR Returns True if either of the
conditions are True
• not (!) Logical NOT Returns the negation of the
condition.
Logical Operators in Python
•Example
Output
Python Program
2) WAP to accept two numbers from the user and display its
sum.
Program Code:
Output:
Addition not taken place
Can you give reason?
Reason
This is because input() function returns the value as
a string. When adding two strings, ‘+’ operator will
act as concatenation operator. i.e., It will join the
values of two strings as a single string. Hence we
got the output as 2030.
Note:
To convert the string as a number use int() function.
int() Function
• int() function converts the value into an integer.
2) WAP to add two numbers accepted from the user.
Program Code:
Output:
Addition taken place
More Python Programs
3) Program to accept length and breadth of a rectangle
and calculate its area and perimeter.
Program Code:
Output:
More Python Programs
4) Program to accept side of a square and calculate its
area and perimeter.
Program Code:
Output:
More Python Programs
5) Program to accept radius of a circle and calculate its
area and circumference.
Program Code:
Output:
More Python Programs
6) Program to accept the values of principal amount,
time period and rate from the user and print SI.
Program Code:
Output:
More Python Programs
7) Program to accept a number from the user and
find it square and cube.
Program Code:
Output:
More Python Programs
8) Program to accept 3 numbers from the user and print
its sum and average.
Program Code:
Output:
More Python Exercises
9) Find the output:
i) iv)
ii) v)
iii)
Answers
9)
i) iv)
ii) v)
iii)