Getting Started with
Python
Sharayu Bonde
sharayubonde29@gmail.com
Python
Overview
2
What is Python?
Python is a popular programming language. It is developed by Guido van Rossum, and
released in 1991.
It is used for:
● web development (server-side),
● software development,
● mathematics,
● system scripting.
Python Overview
3
Why Python?
● Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
● Python has a simple syntax similar to the English language.
● Python has syntax that allows developers to write programs with fewer lines than
some other programming languages.
● Python runs on an interpreter system, meaning that code can be executed as soon as it
is written.
● Python can be treated in a procedural way, an object-oriented way or a functional
way
Python
Installation
Steps
4
● Download python installer from link
https://www.Python.org/downloads/
❏ Download the .tgz file of Python from the above
provided download link
www.python.org/ftp/python.2.7.11/python-2.7.
11.tgz
❏ Unzip the downloaded file.
Tar xvfz Python-2.7.11.tgz
❏ Go to the directory
cd Python-2.7.11
❏ ./configure
❏ Build it.
❏ Make
❏ Su or sudo su if
there is no root
user
❏ Make install
How to
Install
Python 3.3
5
● Visit:
www.python.org
○ Download python for
Linux
○ Extract zip file in home
● Open
Terminal
○ $ sudo apt-get install build-
essential
○ $ sudo apt-get install libsqlite3-
dev
○ $ sudo opt-get install libbz2-dev
Go in Python folder
○ $cd python 3.3.2
○ $python 3.3.2 $ ./configure
○ python 3.3.2 $ make
○ python 3.3.2 $ sudo make
altinstall
Getting Started with
Python
6
● How to write python program
● How to Run python Program?
● Python Indentations ( We will see this later )
Getting Started with
Python
● How to write python program
Ways of Python Programming
1. Command Line
2.Python IDLE ( Integrated Development and
Learning Environment)
7
Command
Line
8
Steps in windows:
● Press the start button
● Click on All programs and then Python 3.8. After clicking on Python 3.8 you will see a list of
options.
● In this list click on Python 3.7. After clicking on it, you will see the python Interactive prompt in
python command line.
OR
● Open command prompt and type python ( After typing python you will enter in
interpreter mode)
Command
Line
9
Steps in Linux:
● Open Terminal
● Type Python ( After typing python you will enter in interpreter mode)
Getting Started with
Python
● How to write python program
Ways of Python Programming
1. Command Line
2.Python IDLE ( Integrated Development and
Learning Environment)
10
IDLE ( Integrated Development and Learning
Environment)
11
Steps in windows:
● Press the start button or Type Python in search
● Click on IDLE(Python 3.8 )
● After clicking on it it will open Python IDLE i.e Python Interactive Prompt
● Click on File
● Click on New. It will open new file there we can type our python Program
● To run press F5. You will get output on interactive prompt.
Getting Started with
Python
● How to write python program
( On Linux OS)
Steps to write Python Program
(On Linux Platform )
1. Open command prompt
2. Go in current folder
3. Type on command prompt
$ gedit sample.py OR $pico sample.py
4. Save file (sample.py)
5. Run file
$ python sample.py
12
Getting Started with
Python
● How to write python program
Sample.py
statement_1
statement_2
statement_3
13
Getting Started with
Python
● How to write python program
Sample.py
14
# Function Calling
statement_1
function_1()
statement_2
function_1()
# Function definition
Getting Started with
Python
● How to write python program
Sample.py
function_1()
# Function definition
15
# Function Calling
statement_1
function_1()
statement_2
Getting Started with
Python
● How to Run python Program?
harish@harish-Inspiron-3537:~$ python file_name.py
16
Getting Started with
Python
● How to Run python Program?
harish@harish-Inspiron-3537:~$ python sample.py
17
Getting Started with
Python
Hands-
on !!
18
Introduction to
Python
19
● Comments
● Python Identifiers
● Reserved Keywords
● Variables
Introduction to
Python
● Comments
Python uses the hash ( # )
character for commenting
>>> 10+20 addition
SyntaxError: invalid syntax
>>>
>>>
>>> 10+20 #Addition
30
20
Introduction to
Python
● Python Identifiers
A python identifier is the name
given to a variable, function, class
or other object.
21
Introduction to
Python
● Reserved Keywords
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as e l i f i f or yield
assert else import pass
break except in raise
22
Python Reserved
Keywords
Introduction to
Python
● Variable
s
● Declaring a Variable
● Initializing a Variable
23
Input and output
Functions
24
1. print()
Function
2. input()
Function
Input and output
Functions
>>> print("Welcome to Python Programming")
Welcome to Python Programming
>>>
25
1. print()
Function
Input and output
Functions
1. input()
Function
>>> a=input()
harish
>>> a
'harish'
>>> a=input()
10
>>> a
'10' 26
Input and output
Functions
10
>>> a=input()
harish
>>> a
'harish'
>>> a=input()
10
>>> a
'10'
1. input() Function
>>> a=int(input())
10
>>> a
10
>>> a=input("Enter your Name: ")
Enter your Name: Harish
>>> a
'Harish'
>>>
Standard Data Types
Standard Data Type
2
9
1. Numeric
2. Boolean
3. String
4. List
5. Tuples
6. Dictionary
7. Sets
1.
Numeric
3
0
● Numeric data comes in two flavours
○ Int - Integer
○ Float - Fractional Numbers
● 150, -5, 564123 are values of integer type
● 10.52, -0.01, 25.23156 are values of type float
1.
Numeric
3
1
● Operations on Numbers
○ Normal arithmetic operations : +, -, *, /
○ Note that, / always produces a float
○ Quotient and Remainder : // and %
○ Exponentiation : **
● Other Operations on Numbers
○ log(), sqrt(), sin(),.....
○ Built-in python but not available by default
○ Must include “math library”
○ from math import *
1.
Numeric
● Operations on Numbers
○ Normal arithmetic operations : +, -, *, /
○ Note that, / always produces a float
○ Quotient and Remainder : // and %
○ Exponentiation : **
● Other Operations on Numbers
○ log(), sqrt(), sin(),.....
○ Built-in python but not available by default
○ Must include “math library”
○ from math import *
>>> 5+8
13
>>> 5/3
1.6666666666666667
>>> 5//2
2
>>> 5%2
1
>>> 5**2
25
3
2
● Operations on Numbers
○ Normal arithmetic operations : +, -, *, /
○ Note that, / always produces a float
○ Quotient and Remainder : // and %
○ Exponentiation : **
● Other Operations on Numbers
○ log(), sqrt(), sin(),.....
○ Built-in python but not available by default
○ Must include “math library”
○ from math import *
1.
Numeric
>>> from math import*
>>> sqrt(4)
2.0
>>> sin(90)
0.8939966636005579
>>> log(2)
0.6931471805599453
>>>
3
3
2. Boolean Values : bool
● True, False
● Logical Operators : nor, and, or
○ Not True is False, Not False is
True
○ x and y is True, if both of x, y are True
○ x or y is True, if atleast one of x,y is
True
>>> a=True
>>> type(a)
<class
'bool'>
>>>
x=False
>>>
type(x)
<class
'bool'> 3
4
3. String
(str)
3
5
● Strings are another important data type in Python.
● Type string is a sequence of character
○ A single character is a string of length 1
○ No separate type char
● Enclose in quotes - Single, double, triple
City = ‘Pune’
Class = “ It’s Harish Gadade’s Class”
Name = ‘ ‘ ‘ It’s name “Python Programming” ‘
‘ ‘
3. String
(str)
-6 -5 -4 -3 -2 -1
●
●
Positions -1,-2, . . . . . count backwards from end
There are several operators such as slicing
[ : repetition ( * )
], concatenation ( + ) and
36
P y t h o n
● String is a sequence of characters
● Position 0,1,2,..... N-1 for a string of length n
○ s = “ Python”
0 1
2
3
4
5
3. String
(str)
P y t h o n
● String is a sequence of characters
● Position 0,1,2,..... N-1 for a string of length n
○ s = “ Python”
0 1
2
3
4
5 ● Strings are Immutable
-6 -5 -4 -3 -2 -1
●
●
Positions -1,-2, . . . . . count backwards from end
There are several operators such as slicing
[ : repetition ( * )
], concatenation ( + ) and
37
4. List
38
● A List can contain same types of Items. Alternatively, a List can also contain
different types of items.
● To declare a List, we need to separate the items using commas and enclose them within
a square bracket( [] )
● Similar to string data type, list also has +, * and slicing [:] operators
for concatenation, repetition and sublist respectively.
● List is Mutable
4. List
39
● Basic Operations
○ Display List
○ Concatenation
○ Repetition
○ Sublist
4. List
● Basic Operations
○ Display List
○ Concatenation
○ Repetition
○ Sublist
>>> a = [ 1, ”Vihaan”, 5.6 ]
>>> b = [ “Pune”, 10 ]
40
4. List
● Basic Operations
○ Display List
○ Concatenation
○ Repetition
○ Sublist
>>> a = [ 1, ”Vihaan”, 5.6 ]
>>> b = [ “Pune”, 10 ]
Displaying a
List
>>> a
a = [ 1, ”Vihaan”, 5.6 ]
>>> b
[ “Pune”, 10 ]
41
4. List
● Basic Operations
○ Display List
○ Concatenation
○ Repetition
○ Sublist >>> a + b
42
>>> a = [ 1, ”Vihaan”, 5.6 ]
a = [ 1, ”Vihaan”, 5.6, “Pune”, 10 ]
>>> b = [ “Pune”, 10 ]
Concatenatio
n
4. List
● Basic Operations
○ Display List
○ Concatenation
○ Repetition
○ Sublist >>> b * 3
43
>>> a = [ 1, ”Vihaan”, 5.6 ]
[ “Pune”, 10 , “Pune”, 10 , “Pune”,
10 ]
>>> b = [ “Pune”, 10 ]
Repetitio
n
4. List
● Basic Operations
○ Display List
○ Concatenation
○ Repetition
○ Sublist
>>> a = [ 1, ”Vihaan”, 5.6 ]
>>> b = [ “Pune”, 10 ]
>>>a [ 0 : 1 ]
44
[1]
>>> a [ 1 : 3 ]
[“Vihaan”, 5.6]
Sub-
List
5.
Tuple
45
square
● Similar to list, a tuple is also used to store sequence of items.
● Like a list, a tuple consists of items separated by commas.
● However, tuples are enclosed within parentheses rather than bracket.
● Difference between List and Tuples
○ In List, items are enclosed within square brackets [ ] whereas in tuples, items
are enclosed within parentheses ()
○ List are mutable whereas Tuples are immutable. Tuples are read only lists.
5.
Tuple
● Examples
>>> a=(10,"Vihaan",5.6,"Jalgaon")
>>> a
(10, 'Vihaan', 5.6, 'Jalgaon')
>>> a[1]="Rituja"
Traceback (most recent call last):
File "<pyshell#4>", line 1, in
<module> a[1]="Rituja"
TypeError: 'tuple' object does not
support item assignment
46
6.
Dictionary
47
● Dictionary is an unordered collection of key-value pairs.
● The order of elements in a dictionary is undefined but we can iterate
over the following:
○ The Key
○ The Value
○ The item ( key - Value pairs) in a dictionary
● Items are enclosed in a curly-braces { } and separated by comma
( , ).
● A colon ( : ) is used to separate key from value.
● A key inside the square bracket [ ] is used to access the dictionary
items
● Dictionary values are mutable
6.
Dictionary
● Example:
>>> dict={1:"Jalgaon","two":"Pune"}
>>> dict
{1: 'Jalgaon', 'two': 'Pune'}
>>> dict[3]="Mumbai"
>>> dict
{1: 'Jalgaon', 'two': 'Pune', 3:
'Mumbai'}
48
6.
Dictionary
● Example:
>>> dict={1:"Jalgaon","two":"Pune"}
>>> dict
{1: 'Jalgaon', 'two': 'Pune'}
>>> dict[3]="Mumbai"
>>> dict
{1: 'Jalgaon', 'two': 'Pune', 3: 'Mumbai'}
>>> dict.keys()
dict_keys([1, 'two', 3])
>>> dict.values()
dict_values(['Jalgaon', 'Pune',
'Mumbai'])
>>>
49
7. Sets
50
● An unordered collection of data is known as set.
● A set does not contain duplicate values or elements and it is
non-subscriptable
● Union, intersection, difference and symmetric difference are
the some operations which can performed on sets.
○ Union: All elements from two sets. Operator used is |
○ Intersection: Display common elements in two sets. Operator used
is &
○ Difference: Display elements which are present in first set not
in other set. Operator used is -
○ Symmetric Difference: returns elements which are
present in either set but not in both. Operator used is ^
7. Sets
● Example:
>>> a = set ( [1,2,3,1,2,8,5,4] )
>>> b = set ( [1,9,3,2,5] )
>>> a
{1, 2, 3, 4, 5, 8}
>>> b
{1, 2, 3, 5, 9}
>>> intersection = a & b
>>> intersection
{1, 2, 3, 5}
>>> union = a | b
>>> union
{1, 2, 3, 4, 5, 8, 9}
>>>
51
7. Sets
● Example:
>>> a = set ( [1,2,3,1,2,8,5,4] )
>>> b = set ( [1,9,3,2,5] )
>>> a
{1, 2, 3, 4, 5, 8}
>>> b
{1, 2, 3, 5, 9}
>>> diff = a - b
>>> diff
{8, 4}
>>> symm_diff = a ^ b
>>> symm_diff
{4, 8, 9}
52
Type( )
Function
53
● type() function is a built-in function which returns the datatype of
any arbitrary object.
● type() function can take anything as an argument and returns its
data type such as integer, strings, dictionaries, lists, classes,
modules, tuples, function etc
Type( )
Function
● type() function is a built-in function which returns the datatype of
any arbitrary object.
● type() function can take anything as an argument and returns its
data type such as integer, strings, dictionaries, lists, classes,
modules, tuples, function etc
>>> x=10
>>> type(x)
<class 'int'>
>>> import os
>>> type(os)
<class 'module'>
>>> type("Hello")
<class 'str'>
>>> tup=(1,2,3)
>>> type(tup)
<class 'tuple'>
>>> lst=[1,2,3]
>>> type(lst)
<class 'list'>
54
Operators in Python
Operator
s
56
● Arithmetic Operators
a. Unary Operator
b. Binary Operator
● Comparison Operators
● Assignment Operators
● Logical Operators
● Membership Operators
● Identity Operators
Operator
s
57
● Arithmetic Operators :
a. Unary Operator: +, -, ~
b. Binary Operator : +, -, *, /, **, %, //
Operator
s
58
● Arithmetic Operators :
a. Unary Operator: +, -, ~
b. Binary Operator : +, -, *, /, **, %, //
● Examples:
○ Write a program to calculate square and cube using * operator
○ Write a program to read a temperature in Celsius and convert it into
Fahrenheit [ Hint: F = (9/5)*Celsius + 32 ]
Operator
s
59
● Home Work:
○ Write a program to calculate the distance between two points.
○ Write a program to reverse a four digit number.
○ Write a program
X
to
Y
display the following table
Result
10 2 100
10 3 1000
10 4 10000
10 5 100000
Operator
s
60
● Arithmetic Operators :
a. Unary Operator: +, -, ~
b. Binary Operator : +, -, *, /, **, %, //
● Comparison Operators : ==, != or <>
● Assignment Operators : =, +=, -=, *=, /=, %=, **=, //=
● Logical Operators : and, or, not
● Membership Operators : in, not in
● Identity Operators: is, is not
Statement and
Expression
● Statement: An instruction that can be interpreted by the Python Interpreter
● Expression: A expression is a combination of variables,operators, values and a reserve
keywords
Statements:
print, while,
for, if etc
Expression:
61
10+20,
x+y
String
Operations
● Concatenation
● Repetition
● Get Particular character
● Slicing
Will see later
62
Conditional Execution
Conditional Execution / Decision Making
Statements
64
● if statements
● if - else statements
● if - elif - else
Statements
If
Statements
65
● Syntax
if
conditio
n :
statement_1
statement_2
If
Statements
● Syntax
if
conditio
n :
statement_1
statement_2
66
If
Statements
● Syntax
if
conditio
n :
statement_1
statement_2
● Example
If m%n != 0:
(m,n
) =
● Statement ( body of if ) will be
executed when if condition is
true...
i.e. m%n !=0 is True
● Indentation demarcates body of
if
● Indentation must be uniform
● : represents end of
if
condition and start body of if
67
If
Statements
● Syntax
if
conditio
n :
statement_1
statement_2
● Example
If m%n != 0:
(m,n
) =
If condition :
Statement_1
Statement_2
Statement_3
68
If - else
Statements
● Syntax:
if condition :
statement_1
Statement_2
else:
Statement_3
Statement_4
● Here else is
Optional 69
If - else
Statements
● Syntax:
if condition :
statement_1
Statement_2
else:
Statement_3
Statement_4
● Here else is
Optional
if m%n != 0:
(m,n) = (n, m%n)
else:
gcd = n
70
If - elif - else
Statements
● Syntax:
if condition1 :
statement_1
statement_2
elif condition2 :
statement_3
statement_4
. . . . .
. . . . .
else :
9
Hands-on!!
72
1. Write a program that accepts two integer numbers and prints
message “ Equals” if both the entered numbers are equal.
2. Write a program that accepts a radius of a circle. If the radius
of a circle is greater than zero then print the area and
circumference of the circle.
3. Write a program that accepts two numbers and finds greater number.
4. Write a program to test whether a number is divisible by 5 and 10
OR 5 or 10.
73
Hands-
on!!
Control Statements
Control Statements
● while Loop
● for Loop
● Break and Continue Statements
2
While
Loop
● While Loops is used to execute a
block of statements repeatedly until a
given condition is satisfied.
● Syntax_1:
while expression:
Statement_1
Statement_2
● Syntax_2:
while condition:
Statement block
else:
Statement Block 3
While Loop - Hands-
on!!
1. Write a program to print the numbers from one to five using while loop
7
7
While Loop - Hands-
on!!
1. Write a program to print the numbers from one to five using while loop
2. WAP to add 10 consecutive numbers starting from 1 using while loop
7
8
While Loop - Hands-
on!!
1. Write a program to print the numbers from one to five using while loop
2. WAP to add 10 consecutive numbers starting from 1 using while loop
3. Write a program to find the sum of the digits of a given number using
while loop
7
9
While Loop - Hands-
on!!
1. Write a program to print the numbers from one to five using while loop
2. WAP to add 10 consecutive numbers starting from 1 using while loop
3. Write a program to find the sum of the digits of a given number using while
loop
4. Write a program to display the reverse of the entered number using while loop
8
0
While Loop - Hands-
on!!
1. Write a program to print the numbers from one to five using while loop
2. WAP to add 10 consecutive numbers starting from 1 using while loop
3. Write a program to find the sum of the digits of a given number using while
loop
4. Write a program to display the reverse of the entered number using while loop
5. WAP to print the sum of the numbers from 1 to 30 (1 and 30 are included) that are
divisible by 5 using while loop
8
1
While Loop - Hands-
on!!
1. Write a program to print the numbers from one to five using while loop
2. WAP to add 10 consecutive numbers starting from 1 using while loop
3. Write a program to find the sum of the digits of a given number using while
loop
4. Write a program to display the reverse of the entered number using while loop
5. WAP to print the sum of the numbers from 1 to 30 (1 and 30 are included) that are
divisible by 5 using while loop
6. WAP to calculate factorial of a given number using while loop 8
2
Range() Function
● Inbuilt Function
● Syntax:
range(begin,end,step)
Where,
Begin : First number
End : is last number
or a limit
Step : difference
between each number
in the sequence
● Example:
[1,2,3,4,5] 10
Control Statements
● while Loop
● for Loop
● Break and Continue Statements
Ways to display f o r loop
- Range() function
- Reverse printing
- Ways to use f o r loop
84
Control
Statements
85
● while Loop
● for Loop
● Break and Continue Statements
Control
Statements
● while Loop
● for Loop
● Break and Continue Statements
f o r i i n range(5):
i f ( i = = 3 ) :
break
p r i n t ( “ H e l l o ” , i )
86
Control
Statements
● while Loop
● for Loop
● Break and Continue Statements
f o r i i n range(5):
i f ( i = = 3 ) :
continue
p r i n t ( “ H e l l o ” , i )
87
Control
Statements
● while Loop
● for Loop
Examples:
2. 1
2 2
3 3 3
4 4 4 4
3. 1
1 2
1 2 3
1 2 3 4
4. 1
2 1
3 2 1
4 3 2 1
Examples:
● Break and Continue Statements 1. 0 0 0 0
1 1 1 1
2 2 2 2
3 3 3 3
88
Homewor
k
89
1. Write a program to find the square root of a number
2. Write a program to find area of a rectangle
3. Write a program to swap the values of two variables
4. Write a program to convert kilogram into pound
5. Write a program to find whether a number is even or odd
6. Write a program to check the largest among the given three
numbers

Python chapter presentation details.pptx

  • 1.
    Getting Started with Python SharayuBonde sharayubonde29@gmail.com
  • 2.
    Python Overview 2 What is Python? Pythonis a popular programming language. It is developed by Guido van Rossum, and released in 1991. It is used for: ● web development (server-side), ● software development, ● mathematics, ● system scripting.
  • 3.
    Python Overview 3 Why Python? ●Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc). ● Python has a simple syntax similar to the English language. ● Python has syntax that allows developers to write programs with fewer lines than some other programming languages. ● Python runs on an interpreter system, meaning that code can be executed as soon as it is written. ● Python can be treated in a procedural way, an object-oriented way or a functional way
  • 4.
    Python Installation Steps 4 ● Download pythoninstaller from link https://www.Python.org/downloads/ ❏ Download the .tgz file of Python from the above provided download link www.python.org/ftp/python.2.7.11/python-2.7. 11.tgz ❏ Unzip the downloaded file. Tar xvfz Python-2.7.11.tgz ❏ Go to the directory cd Python-2.7.11 ❏ ./configure ❏ Build it. ❏ Make ❏ Su or sudo su if there is no root user ❏ Make install
  • 5.
    How to Install Python 3.3 5 ●Visit: www.python.org ○ Download python for Linux ○ Extract zip file in home ● Open Terminal ○ $ sudo apt-get install build- essential ○ $ sudo apt-get install libsqlite3- dev ○ $ sudo opt-get install libbz2-dev Go in Python folder ○ $cd python 3.3.2 ○ $python 3.3.2 $ ./configure ○ python 3.3.2 $ make ○ python 3.3.2 $ sudo make altinstall
  • 6.
    Getting Started with Python 6 ●How to write python program ● How to Run python Program? ● Python Indentations ( We will see this later )
  • 7.
    Getting Started with Python ●How to write python program Ways of Python Programming 1. Command Line 2.Python IDLE ( Integrated Development and Learning Environment) 7
  • 8.
    Command Line 8 Steps in windows: ●Press the start button ● Click on All programs and then Python 3.8. After clicking on Python 3.8 you will see a list of options. ● In this list click on Python 3.7. After clicking on it, you will see the python Interactive prompt in python command line. OR ● Open command prompt and type python ( After typing python you will enter in interpreter mode)
  • 9.
    Command Line 9 Steps in Linux: ●Open Terminal ● Type Python ( After typing python you will enter in interpreter mode)
  • 10.
    Getting Started with Python ●How to write python program Ways of Python Programming 1. Command Line 2.Python IDLE ( Integrated Development and Learning Environment) 10
  • 11.
    IDLE ( IntegratedDevelopment and Learning Environment) 11 Steps in windows: ● Press the start button or Type Python in search ● Click on IDLE(Python 3.8 ) ● After clicking on it it will open Python IDLE i.e Python Interactive Prompt ● Click on File ● Click on New. It will open new file there we can type our python Program ● To run press F5. You will get output on interactive prompt.
  • 12.
    Getting Started with Python ●How to write python program ( On Linux OS) Steps to write Python Program (On Linux Platform ) 1. Open command prompt 2. Go in current folder 3. Type on command prompt $ gedit sample.py OR $pico sample.py 4. Save file (sample.py) 5. Run file $ python sample.py 12
  • 13.
    Getting Started with Python ●How to write python program Sample.py statement_1 statement_2 statement_3 13
  • 14.
    Getting Started with Python ●How to write python program Sample.py 14 # Function Calling statement_1 function_1() statement_2 function_1() # Function definition
  • 15.
    Getting Started with Python ●How to write python program Sample.py function_1() # Function definition 15 # Function Calling statement_1 function_1() statement_2
  • 16.
    Getting Started with Python ●How to Run python Program? harish@harish-Inspiron-3537:~$ python file_name.py 16
  • 17.
    Getting Started with Python ●How to Run python Program? harish@harish-Inspiron-3537:~$ python sample.py 17
  • 18.
  • 19.
    Introduction to Python 19 ● Comments ●Python Identifiers ● Reserved Keywords ● Variables
  • 20.
    Introduction to Python ● Comments Pythonuses the hash ( # ) character for commenting >>> 10+20 addition SyntaxError: invalid syntax >>> >>> >>> 10+20 #Addition 30 20
  • 21.
    Introduction to Python ● PythonIdentifiers A python identifier is the name given to a variable, function, class or other object. 21
  • 22.
    Introduction to Python ● ReservedKeywords False class finally is return None continue for lambda try True def from nonlocal while and del global not with as e l i f i f or yield assert else import pass break except in raise 22 Python Reserved Keywords
  • 23.
    Introduction to Python ● Variable s ●Declaring a Variable ● Initializing a Variable 23
  • 24.
    Input and output Functions 24 1.print() Function 2. input() Function
  • 25.
    Input and output Functions >>>print("Welcome to Python Programming") Welcome to Python Programming >>> 25 1. print() Function
  • 26.
    Input and output Functions 1.input() Function >>> a=input() harish >>> a 'harish' >>> a=input() 10 >>> a '10' 26
  • 27.
    Input and output Functions 10 >>>a=input() harish >>> a 'harish' >>> a=input() 10 >>> a '10' 1. input() Function >>> a=int(input()) 10 >>> a 10 >>> a=input("Enter your Name: ") Enter your Name: Harish >>> a 'Harish' >>>
  • 28.
  • 29.
    Standard Data Type 2 9 1.Numeric 2. Boolean 3. String 4. List 5. Tuples 6. Dictionary 7. Sets
  • 30.
    1. Numeric 3 0 ● Numeric datacomes in two flavours ○ Int - Integer ○ Float - Fractional Numbers ● 150, -5, 564123 are values of integer type ● 10.52, -0.01, 25.23156 are values of type float
  • 31.
    1. Numeric 3 1 ● Operations onNumbers ○ Normal arithmetic operations : +, -, *, / ○ Note that, / always produces a float ○ Quotient and Remainder : // and % ○ Exponentiation : ** ● Other Operations on Numbers ○ log(), sqrt(), sin(),..... ○ Built-in python but not available by default ○ Must include “math library” ○ from math import *
  • 32.
    1. Numeric ● Operations onNumbers ○ Normal arithmetic operations : +, -, *, / ○ Note that, / always produces a float ○ Quotient and Remainder : // and % ○ Exponentiation : ** ● Other Operations on Numbers ○ log(), sqrt(), sin(),..... ○ Built-in python but not available by default ○ Must include “math library” ○ from math import * >>> 5+8 13 >>> 5/3 1.6666666666666667 >>> 5//2 2 >>> 5%2 1 >>> 5**2 25 3 2
  • 33.
    ● Operations onNumbers ○ Normal arithmetic operations : +, -, *, / ○ Note that, / always produces a float ○ Quotient and Remainder : // and % ○ Exponentiation : ** ● Other Operations on Numbers ○ log(), sqrt(), sin(),..... ○ Built-in python but not available by default ○ Must include “math library” ○ from math import * 1. Numeric >>> from math import* >>> sqrt(4) 2.0 >>> sin(90) 0.8939966636005579 >>> log(2) 0.6931471805599453 >>> 3 3
  • 34.
    2. Boolean Values: bool ● True, False ● Logical Operators : nor, and, or ○ Not True is False, Not False is True ○ x and y is True, if both of x, y are True ○ x or y is True, if atleast one of x,y is True >>> a=True >>> type(a) <class 'bool'> >>> x=False >>> type(x) <class 'bool'> 3 4
  • 35.
    3. String (str) 3 5 ● Stringsare another important data type in Python. ● Type string is a sequence of character ○ A single character is a string of length 1 ○ No separate type char ● Enclose in quotes - Single, double, triple City = ‘Pune’ Class = “ It’s Harish Gadade’s Class” Name = ‘ ‘ ‘ It’s name “Python Programming” ‘ ‘ ‘
  • 36.
    3. String (str) -6 -5-4 -3 -2 -1 ● ● Positions -1,-2, . . . . . count backwards from end There are several operators such as slicing [ : repetition ( * ) ], concatenation ( + ) and 36 P y t h o n ● String is a sequence of characters ● Position 0,1,2,..... N-1 for a string of length n ○ s = “ Python” 0 1 2 3 4 5
  • 37.
    3. String (str) P yt h o n ● String is a sequence of characters ● Position 0,1,2,..... N-1 for a string of length n ○ s = “ Python” 0 1 2 3 4 5 ● Strings are Immutable -6 -5 -4 -3 -2 -1 ● ● Positions -1,-2, . . . . . count backwards from end There are several operators such as slicing [ : repetition ( * ) ], concatenation ( + ) and 37
  • 38.
    4. List 38 ● AList can contain same types of Items. Alternatively, a List can also contain different types of items. ● To declare a List, we need to separate the items using commas and enclose them within a square bracket( [] ) ● Similar to string data type, list also has +, * and slicing [:] operators for concatenation, repetition and sublist respectively. ● List is Mutable
  • 39.
    4. List 39 ● BasicOperations ○ Display List ○ Concatenation ○ Repetition ○ Sublist
  • 40.
    4. List ● BasicOperations ○ Display List ○ Concatenation ○ Repetition ○ Sublist >>> a = [ 1, ”Vihaan”, 5.6 ] >>> b = [ “Pune”, 10 ] 40
  • 41.
    4. List ● BasicOperations ○ Display List ○ Concatenation ○ Repetition ○ Sublist >>> a = [ 1, ”Vihaan”, 5.6 ] >>> b = [ “Pune”, 10 ] Displaying a List >>> a a = [ 1, ”Vihaan”, 5.6 ] >>> b [ “Pune”, 10 ] 41
  • 42.
    4. List ● BasicOperations ○ Display List ○ Concatenation ○ Repetition ○ Sublist >>> a + b 42 >>> a = [ 1, ”Vihaan”, 5.6 ] a = [ 1, ”Vihaan”, 5.6, “Pune”, 10 ] >>> b = [ “Pune”, 10 ] Concatenatio n
  • 43.
    4. List ● BasicOperations ○ Display List ○ Concatenation ○ Repetition ○ Sublist >>> b * 3 43 >>> a = [ 1, ”Vihaan”, 5.6 ] [ “Pune”, 10 , “Pune”, 10 , “Pune”, 10 ] >>> b = [ “Pune”, 10 ] Repetitio n
  • 44.
    4. List ● BasicOperations ○ Display List ○ Concatenation ○ Repetition ○ Sublist >>> a = [ 1, ”Vihaan”, 5.6 ] >>> b = [ “Pune”, 10 ] >>>a [ 0 : 1 ] 44 [1] >>> a [ 1 : 3 ] [“Vihaan”, 5.6] Sub- List
  • 45.
    5. Tuple 45 square ● Similar tolist, a tuple is also used to store sequence of items. ● Like a list, a tuple consists of items separated by commas. ● However, tuples are enclosed within parentheses rather than bracket. ● Difference between List and Tuples ○ In List, items are enclosed within square brackets [ ] whereas in tuples, items are enclosed within parentheses () ○ List are mutable whereas Tuples are immutable. Tuples are read only lists.
  • 46.
    5. Tuple ● Examples >>> a=(10,"Vihaan",5.6,"Jalgaon") >>>a (10, 'Vihaan', 5.6, 'Jalgaon') >>> a[1]="Rituja" Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> a[1]="Rituja" TypeError: 'tuple' object does not support item assignment 46
  • 47.
    6. Dictionary 47 ● Dictionary isan unordered collection of key-value pairs. ● The order of elements in a dictionary is undefined but we can iterate over the following: ○ The Key ○ The Value ○ The item ( key - Value pairs) in a dictionary ● Items are enclosed in a curly-braces { } and separated by comma ( , ). ● A colon ( : ) is used to separate key from value. ● A key inside the square bracket [ ] is used to access the dictionary items ● Dictionary values are mutable
  • 48.
    6. Dictionary ● Example: >>> dict={1:"Jalgaon","two":"Pune"} >>>dict {1: 'Jalgaon', 'two': 'Pune'} >>> dict[3]="Mumbai" >>> dict {1: 'Jalgaon', 'two': 'Pune', 3: 'Mumbai'} 48
  • 49.
    6. Dictionary ● Example: >>> dict={1:"Jalgaon","two":"Pune"} >>>dict {1: 'Jalgaon', 'two': 'Pune'} >>> dict[3]="Mumbai" >>> dict {1: 'Jalgaon', 'two': 'Pune', 3: 'Mumbai'} >>> dict.keys() dict_keys([1, 'two', 3]) >>> dict.values() dict_values(['Jalgaon', 'Pune', 'Mumbai']) >>> 49
  • 50.
    7. Sets 50 ● Anunordered collection of data is known as set. ● A set does not contain duplicate values or elements and it is non-subscriptable ● Union, intersection, difference and symmetric difference are the some operations which can performed on sets. ○ Union: All elements from two sets. Operator used is | ○ Intersection: Display common elements in two sets. Operator used is & ○ Difference: Display elements which are present in first set not in other set. Operator used is - ○ Symmetric Difference: returns elements which are present in either set but not in both. Operator used is ^
  • 51.
    7. Sets ● Example: >>>a = set ( [1,2,3,1,2,8,5,4] ) >>> b = set ( [1,9,3,2,5] ) >>> a {1, 2, 3, 4, 5, 8} >>> b {1, 2, 3, 5, 9} >>> intersection = a & b >>> intersection {1, 2, 3, 5} >>> union = a | b >>> union {1, 2, 3, 4, 5, 8, 9} >>> 51
  • 52.
    7. Sets ● Example: >>>a = set ( [1,2,3,1,2,8,5,4] ) >>> b = set ( [1,9,3,2,5] ) >>> a {1, 2, 3, 4, 5, 8} >>> b {1, 2, 3, 5, 9} >>> diff = a - b >>> diff {8, 4} >>> symm_diff = a ^ b >>> symm_diff {4, 8, 9} 52
  • 53.
    Type( ) Function 53 ● type()function is a built-in function which returns the datatype of any arbitrary object. ● type() function can take anything as an argument and returns its data type such as integer, strings, dictionaries, lists, classes, modules, tuples, function etc
  • 54.
    Type( ) Function ● type()function is a built-in function which returns the datatype of any arbitrary object. ● type() function can take anything as an argument and returns its data type such as integer, strings, dictionaries, lists, classes, modules, tuples, function etc >>> x=10 >>> type(x) <class 'int'> >>> import os >>> type(os) <class 'module'> >>> type("Hello") <class 'str'> >>> tup=(1,2,3) >>> type(tup) <class 'tuple'> >>> lst=[1,2,3] >>> type(lst) <class 'list'> 54
  • 55.
  • 56.
    Operator s 56 ● Arithmetic Operators a.Unary Operator b. Binary Operator ● Comparison Operators ● Assignment Operators ● Logical Operators ● Membership Operators ● Identity Operators
  • 57.
    Operator s 57 ● Arithmetic Operators: a. Unary Operator: +, -, ~ b. Binary Operator : +, -, *, /, **, %, //
  • 58.
    Operator s 58 ● Arithmetic Operators: a. Unary Operator: +, -, ~ b. Binary Operator : +, -, *, /, **, %, // ● Examples: ○ Write a program to calculate square and cube using * operator ○ Write a program to read a temperature in Celsius and convert it into Fahrenheit [ Hint: F = (9/5)*Celsius + 32 ]
  • 59.
    Operator s 59 ● Home Work: ○Write a program to calculate the distance between two points. ○ Write a program to reverse a four digit number. ○ Write a program X to Y display the following table Result 10 2 100 10 3 1000 10 4 10000 10 5 100000
  • 60.
    Operator s 60 ● Arithmetic Operators: a. Unary Operator: +, -, ~ b. Binary Operator : +, -, *, /, **, %, // ● Comparison Operators : ==, != or <> ● Assignment Operators : =, +=, -=, *=, /=, %=, **=, //= ● Logical Operators : and, or, not ● Membership Operators : in, not in ● Identity Operators: is, is not
  • 61.
    Statement and Expression ● Statement:An instruction that can be interpreted by the Python Interpreter ● Expression: A expression is a combination of variables,operators, values and a reserve keywords Statements: print, while, for, if etc Expression: 61 10+20, x+y
  • 62.
    String Operations ● Concatenation ● Repetition ●Get Particular character ● Slicing Will see later 62
  • 63.
  • 64.
    Conditional Execution /Decision Making Statements 64 ● if statements ● if - else statements ● if - elif - else Statements
  • 65.
  • 66.
  • 67.
    If Statements ● Syntax if conditio n : statement_1 statement_2 ●Example If m%n != 0: (m,n ) = ● Statement ( body of if ) will be executed when if condition is true... i.e. m%n !=0 is True ● Indentation demarcates body of if ● Indentation must be uniform ● : represents end of if condition and start body of if 67
  • 68.
    If Statements ● Syntax if conditio n : statement_1 statement_2 ●Example If m%n != 0: (m,n ) = If condition : Statement_1 Statement_2 Statement_3 68
  • 69.
    If - else Statements ●Syntax: if condition : statement_1 Statement_2 else: Statement_3 Statement_4 ● Here else is Optional 69
  • 70.
    If - else Statements ●Syntax: if condition : statement_1 Statement_2 else: Statement_3 Statement_4 ● Here else is Optional if m%n != 0: (m,n) = (n, m%n) else: gcd = n 70
  • 71.
    If - elif- else Statements ● Syntax: if condition1 : statement_1 statement_2 elif condition2 : statement_3 statement_4 . . . . . . . . . . else : 9
  • 72.
  • 73.
    1. Write aprogram that accepts two integer numbers and prints message “ Equals” if both the entered numbers are equal. 2. Write a program that accepts a radius of a circle. If the radius of a circle is greater than zero then print the area and circumference of the circle. 3. Write a program that accepts two numbers and finds greater number. 4. Write a program to test whether a number is divisible by 5 and 10 OR 5 or 10. 73 Hands- on!!
  • 74.
  • 75.
    Control Statements ● whileLoop ● for Loop ● Break and Continue Statements 2
  • 76.
    While Loop ● While Loopsis used to execute a block of statements repeatedly until a given condition is satisfied. ● Syntax_1: while expression: Statement_1 Statement_2 ● Syntax_2: while condition: Statement block else: Statement Block 3
  • 77.
    While Loop -Hands- on!! 1. Write a program to print the numbers from one to five using while loop 7 7
  • 78.
    While Loop -Hands- on!! 1. Write a program to print the numbers from one to five using while loop 2. WAP to add 10 consecutive numbers starting from 1 using while loop 7 8
  • 79.
    While Loop -Hands- on!! 1. Write a program to print the numbers from one to five using while loop 2. WAP to add 10 consecutive numbers starting from 1 using while loop 3. Write a program to find the sum of the digits of a given number using while loop 7 9
  • 80.
    While Loop -Hands- on!! 1. Write a program to print the numbers from one to five using while loop 2. WAP to add 10 consecutive numbers starting from 1 using while loop 3. Write a program to find the sum of the digits of a given number using while loop 4. Write a program to display the reverse of the entered number using while loop 8 0
  • 81.
    While Loop -Hands- on!! 1. Write a program to print the numbers from one to five using while loop 2. WAP to add 10 consecutive numbers starting from 1 using while loop 3. Write a program to find the sum of the digits of a given number using while loop 4. Write a program to display the reverse of the entered number using while loop 5. WAP to print the sum of the numbers from 1 to 30 (1 and 30 are included) that are divisible by 5 using while loop 8 1
  • 82.
    While Loop -Hands- on!! 1. Write a program to print the numbers from one to five using while loop 2. WAP to add 10 consecutive numbers starting from 1 using while loop 3. Write a program to find the sum of the digits of a given number using while loop 4. Write a program to display the reverse of the entered number using while loop 5. WAP to print the sum of the numbers from 1 to 30 (1 and 30 are included) that are divisible by 5 using while loop 6. WAP to calculate factorial of a given number using while loop 8 2
  • 83.
    Range() Function ● InbuiltFunction ● Syntax: range(begin,end,step) Where, Begin : First number End : is last number or a limit Step : difference between each number in the sequence ● Example: [1,2,3,4,5] 10
  • 84.
    Control Statements ● whileLoop ● for Loop ● Break and Continue Statements Ways to display f o r loop - Range() function - Reverse printing - Ways to use f o r loop 84
  • 85.
    Control Statements 85 ● while Loop ●for Loop ● Break and Continue Statements
  • 86.
    Control Statements ● while Loop ●for Loop ● Break and Continue Statements f o r i i n range(5): i f ( i = = 3 ) : break p r i n t ( “ H e l l o ” , i ) 86
  • 87.
    Control Statements ● while Loop ●for Loop ● Break and Continue Statements f o r i i n range(5): i f ( i = = 3 ) : continue p r i n t ( “ H e l l o ” , i ) 87
  • 88.
    Control Statements ● while Loop ●for Loop Examples: 2. 1 2 2 3 3 3 4 4 4 4 3. 1 1 2 1 2 3 1 2 3 4 4. 1 2 1 3 2 1 4 3 2 1 Examples: ● Break and Continue Statements 1. 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 88
  • 89.
    Homewor k 89 1. Write aprogram to find the square root of a number 2. Write a program to find area of a rectangle 3. Write a program to swap the values of two variables 4. Write a program to convert kilogram into pound 5. Write a program to find whether a number is even or odd 6. Write a program to check the largest among the given three numbers