SlideShare a Scribd company logo
Faculty of Engineering Science & Technology(FEST)
Hamdard Institute of Engineering Technology(HIET)
Hamdard University
Instructor
ABDUL HASEEB
HANDS-ON WORKSHOP ON PYTHON
PROGRAMMING LANGUAGE
(Day -1)
Faculty Development Program (Session-10)
The Python Programming Language
Audience
• Having basic understanding of programming or worked
on any High Level language like C, JAVA, or C ++, etc
At the end of today`s session you will be able to:
• Understand the significance of Python Programming
• Familiarize with the Python IDE Environment
• Understand the Input and Output operations in Python
• Use variables in Python language
What is Python Programming
• Python was developed by Guido van Rossum
(a Dutch programmer) in 1991
• Python is a High Level Language (whereas C and
Java are not High Level Language)
• Python has a design philosophy that emphasizes
Code Readability
• The syntax allows programmer to express
concepts in fewer lines of code than might be
used in languages such as C++, Java etc
Why Python Programming
According to IEEE Spectrum the top Programming Languages in
2017 are:
http://spectrum.ieee.org/computing/software/the-2017-top-programming-languages
Because it Easy to learn and use: Its simple
syntax is very accessible to programming
novices and will look familiar to anyone with
experience in Matlab, C/C++, Java. Also
Python has powerful libraries
Why Python Programming
• Because it is FREE
There is no worried of buying the key and or unethical use of software by
cracking its key
• Because it is Open Source
Why Python Programming
In the Open Source software the
source code, blue print and
documentation are freely available to
public. The software developer
voluntarily put their part to add the
features in the software
Python has vast development implementation
Python Cross-compilers to other languages
Python Web development frameworks
Python in other Science
Python has Powerful Libraries
Who uses Python
How to Learn Python
Working on Python IDLE
Download Python IDLE 2.x or 3.x from Python Software Foundation
Website for Free
Python IDLE Environment
IDLE (Integrated Development Learning Environment)
• IDLE has multi-window text editor with syntax highlighting,
auto-completion, smart indent
• The Shell window execute command at the statement
complete
• User code written in Text Editor window
Some Useful command for Python IDLE Shell
window
• Alt + p to retrieve Previous Command
• Alt + n to retrieve Next Command
• To get help of instruction or command use
help(instruction)
Python IDLE Environment
My First Python Program
Open Python IDLE Shell window and write the following
instruction
A print is an output instruction use to display string or
data value on screen
print("Hamdard University")
Note: Program contain no header file, no main() function,
no semi column at instruction end, even you write string
in single quotation mark. BUT one thing common with
other programming language that is Case Sensitive
print('Hamdard University‘)
Output Instruction
Escape Sequence Working
 Backslash ()
' Single quote (')
" Double quote (")
n ASCII Linefeed (LF)
t ASCII Horizontal Tab (TAB)
Example:
>>>print('BackslashtTabnNewline')
Backslash Tab
Newline`
The following are the Escape Sequence use with
print instruction
Python Data types
Python has five standard data types
• Numbers(Integer and Float)
• String
• List (Array)
• Tuple (Constant Array)
• Dictionary
Variable Number type:
• Integer (whole number)
• Float (decimal point number)
• Character/String (ASCII format)
• Boolean (True and False)
Python Variable
• Type of Variable: Check the type of Variable
• Deleting Variable: Delete the declare variable
Syntax: del(variable_name)
>>> a = 2
>>> print(a)
2
>>> del(a)
>>> print(a)
NameError: name 'a' is not defined
Variable value Type command Result
a=2 type(a) <class 'int'>
b=3.5 type(b) <class 'float'>
c=’f’ type(c) <class 'str'>
d=’abc123’ type(d) <class 'str'>
type(True) <class 'bool'>
type(False) <class 'bool'>
Python is an Interpreter Language
Working Compiler Interpreter
Converting code Convert whole code Step by step
Translation time Slow Fast
Execution time Fast Slow
Programming Languages
C, Visual Basic, Java,
LabView etc
Python, Matlab, PHP, MS
Excel etc
Python Interpreter Example
In Interpreter, if code contains error it may run
partially and stop when error occurs. In the
following code Error is present in line number 7 that
is “d=B” where ‘B’ is not define before, so program
runs and execute up to that line.
Input Instruction
• Input Instruction: To take an input from user through
keyboard input() command is used, this instruction take
input from keyboard in the form of ASCII character and
store it the variable.
• Syntax:
take_input = input(1 Argument)
• Example:
name = input('Enter your name =')
age = input('Enter your age = ')
print('Your name is ', name , ',and your age is = ',age)
Python Version
• Python Version 1 is obsolete now
• Python Version 2 and Python Version 3 is active
• The Python 2 is old version but retain for programming
because lots of module was developed on it which not
work with Python 3
• There is only some difference in execution of
instructions other wise both have same working
Difference between Python 2 and Python 3
Output Instruction
Python 2 Python 3
Python 2 allows to write the print
instruction argument with in Round
bracket ( and) or with out it.
The argument of the print instruction must
written with in Round bracket ( and)
>>> print('String') #Allow
String
>>> print("String") #Allow
String
>>> print 'String' #Allow
String
>>> print "String" #Allow
String
>>> print('String') #Allow
String
>>> print("String") #Allow
String
>>> print 'String' #NotAllow
SyntaxError: Missing parentheses in call to 'print‘
>>> print "String" #NotAllow
SyntaxError: Missing parentheses in call to 'print‘
Difference between Python 2 and Python 3
Input Instruction
Python 2 Python 3
input(prompt) raw_input(prompt) Input(prompt)
It keeps the type of user
enter data
Automatically update the
Input variable to the user
enter data
Always take data in the
string format regardless of
the user enter data
Syntax:
Input_variable = input(prompt)
Input_variable data type is
same as user enter data
Syntax:
Input_variable = input(prompt)
Input_variable is String
Syntax:
Input_variable = input(prompt)
Input_variable is String
Input Instruction Example
Python 2 Python 3
input(prompt) raw_input(prompt) Input(prompt)
>>> a = input("Enter = ")
Enter = 12
>>> type(a)
<type 'int'>
>>> a = input("Enter = ")
Enter = 12.12
>>> type(a)
<type 'float'>
>>> a = input("Enter = ")
Enter = "Ab12"
>>> type(a)
<type 'str'>
>>> a = input("Enter = ")
Enter = Ab12
NameError: name 'Ab12' is not
defined
>>> a = raw_input("Enter = ")
Enter = 12
>>> type(a)
<type 'str'>
>>> a = raw_input("Enter = ")
Enter = 12.12
>>> type(a)
<type 'str'>
>>> a = raw_input("Enter = ")
Enter = "Ab12"
>>> type(a)
<type 'str'>
>>> a = raw_input("Enter = ")
Enter = Ab12
>>> type(a)
<type 'str'>
>>> a = input("Enter = ")
Enter = 12
>>> type(a)
<class 'str'>
>>> a = input("Enter = ")
Enter = 12.12
>>> type(a)
<class 'str'>
>>> a = input("Enter = ")
Enter = "Ab12"
>>> type(a)
<class 'str'>
>>> a = input("Enter = ")
Enter = Ab12
>>> type(a)
<class 'str'>
Difference between Python 2 and Python 3
Difference between Python 2 and Python 3
(problem and its solution)
Python 2 Python 3
Program
num = input('Enter a number = ')
print num,‘x 3 = ',num * 3
num = input('Enter a number = ')
print( num,‘x 3 = ',num * 3 )
Output
Enter a number = 5
5 x 3 = 15
Enter a number = 5
5 x 3 = 555
Observation: the num is integer so
number 5 x 3 is number 15
Observation: the num is String so String 5
x 3 is three times of number 5
The problem in the output of Python 3 is
solved in later slides
Change the variable type
Function Description
Example
Instruction Output
str(x) Converts object x to a string representation.
str(75)
str("25.25")
‘75’
'25.25'
int(x) Converts object x to a integer number.
int(35.26)
int(100.101 )
35
100
float(x) Converts object x to a float number.
float(35)
float(100)
35.0
100.0
list(s) Converts s to a list.
list(range(5,10))
list(range(1,10,2))
[5, 6, 7, 8, 9]
[1, 3, 5, 7, 9]
tuple(s) Converts s to a tuple.
tuple(range(5,10))
tuple(range(1,10,2))
(5, 6, 7, 8, 9)
(1, 3, 5, 7, 9)
dict(d)
Creates a dictionary. d must be a sequence
of (key,value) tuples.
chr(x) Converts an integer to a character.
chr(65)
chr(97)
'A‘
‘a’
ord(x) Converts a single character to its ASCII (integer value)
ord('A')
ord(‘a')
65
97
hex(x) Converts an integer to a hexadecimal string.
hex(16)
hex(100)
'0x10‘
'0x64'
oct(x) Converts an integer to an octal string.
oct(9)
oct(50)
'0o11‘
'0o62'
Exercise
Rewrite the following program in Python 3 with
correct output display
Without changing Variable Type Changing Variable Type
num = input(“Enter a number = “)
print( num,”x 3 = “,num * 3 )
num = input(“Enter a number = “)
print( num,“x 3 = ",int(num) * 3 )
3 x 3 = 333 3 x 3 = 9
Python IDEs
• Python IDLE
• PyCharm
• Sublime Text
• Atom
• Geany
• Anjuta
• Eric
• Komodo IDE
• KDevelop
• Ninja-IDE
•
• Spyder
Integer Operation
Function Description
bit_length(...)
Number of bits necessary to represent self in binary
Example: >>> (37).bit_length() 6
>>> int_num.bit_length() 2
bin(…)
Return binary of the integer
Example: >>> bin(37) '0b100101'
>>> bin(25) '0b1111111'
Denominator The denominator of a rational number in lowest terms
Imag The imaginary part of a complex number
numerator The numerator of a rational number in lowest terms
real The real part of a complex number
conjugate(...) Returns self, the complex conjugate of any int.
to_bytes(...) Return an array of bytes representing an integer.
Float Operation
Function Description
float.fromhex (...)
Create a floating-point number
from a hexadecimal string.
Example: >>>float.fromhex('0x1.ffffp10') 2047.984375
float.hex(…)
Return a hexadecimal representation of a floating-point number
Example: >>>3.14159.hex() '0x1.921f9f01b866ep+1'
is_integer(...)
Return True if the float is an integer
Example: >>> num = 5.5
>>> num.is_integer() False
>>> num = num - 0.5 # remove floating value
>>> num.is_integer() True
as_integer_ratio(...)
Return a pair of integers, whose ratio is exactly equal to the original float and
with a positive denominator.
Example: >>> (10.0).as_integer_ratio() (10, 1)
>>> (0.0).as_integer_ratio() (0, 1)
>>> (-.25).as_integer_ratio() (-1, 4)
conjugate(...) Return self, the complex conjugate of any float.
imag The imaginary part of a complex number
Real The real part of a complex number
String Operation
Function Description
len(string) Return length of string
capitalize(...)
Return a capitalized version of S, i.e. make the first character have upper
case and the rest lower case.
title(...)
Return a title cased version of S, i.e. words start with title case characters,
and all remaining cased characters have lower case.
lower(...) Return a copy of the string S converted to lowercase.
upper(...) Return a copy of S converted to uppercase.
casefold(...) Return a version of S suitable for caseless comparisons
islower(...)
Return True if all cased characters in S are lowercase and there is at least one
cased character in S, False otherwise.
isupper(...)
Return True if all cased characters in S are uppercase and there is at least
one cased character in S, False otherwise.
isnumeric(...) Return True if there are only numeric characters in S, False otherwise.
isdecimal(...) Return True if there are only decimal characters in S, False otherwise
isdigit(...)
Return True if all characters in S are digits and there is at least one character
in S, False otherwise.
a = input("Enter your name:")
print("Lower Case of Enter string = " ,a.lower())
print("Upper Case of Enter string = " , a.upper())
print("Title Case of Enter string = " , a.title())
print("Length of Enter string = " , len(a))
print("Your Enter string is Alpha = " , a.isalpha())
print("Your Enter string is Decimal = " , a.isdecimal())
print("Your Enter string is Digit = " , a.isdigit())
print("Your Enter string is in Lower case = " , a.islower())
print("Your Enter string is in Upper case = " , a.isupper())
print("Your Enter string is in Title case = " , a.istitle())
Output 1:
Enter your name:Hamdard university
Lower Case of Enter string = hamdard university
Upper Case of Enter string = HAMDARD UNIVERSITY
Title Case of Enter string = Hamdard University
Length of Enter string = 18
Your Enter string is Alpha = False
Your Enter string is Decimal = False
Your Enter string is Digit = False
Your Enter string is in Lower case = False
Your Enter string is in Upper case = False
Your Enter string is in Title case = False
Output 2:
Enter your name:123456
Lower Case of Enter string = 123456
Upper Case of Enter string = 123456
Title Case of Enter string = 123456
Length of Enter string = 6
Your Enter string is Alpha = False
Your Enter string is Decimal = True
Your Enter string is Digit = True
Your Enter string is in Lower case = False
Your Enter string is in Upper case = False
Your Enter string is in Title case = False
String Operation Example
Comparing Python with C/C++
Programming Parameter Python Language C/C++ Language
Programming type Interpreter Compiler
Programming Language High level Language Middle level Language
Header file import #include
Code block Whitespace Indentation
Code within curly bracket
{ code}
Single line statement
termination
Nothing Semicolon ;
Control flow (Multi line)
statement termination
Colon : Nothing
Single line comments # comments //comments
Multi-line comments
‘’’
Comments lines
‘’’
/*
Comments lines
*/
If error code found Partial run code until find error
Did not run until error is
present
Program length Take fewer lines Take relatively more lines
THANK YOU

More Related Content

What's hot (20)

PPTX
Introduction to php
shanmukhareddy dasi
 
PPTX
C# lecture 2: Literals , Variables and Data Types in C#
Dr.Neeraj Kumar Pandey
 
PPSX
Javascript variables and datatypes
Varun C M
 
PDF
Operators in python
eShikshak
 
PDF
Java 8 Lambda Expressions
Scott Leberknight
 
PPTX
Input output files in java
Kavitha713564
 
PDF
Generics
Ravi_Kant_Sahu
 
PPTX
JavaFX Presentation
Mochamad Taufik Mulyadi
 
PPTX
Android Layout
mcanotes
 
PDF
Variables & Data Types In Python | Edureka
Edureka!
 
PDF
Classes and objects
Nilesh Dalvi
 
PDF
Python Course | Python Programming | Python Tutorial | Python Training | Edureka
Edureka!
 
PPT
Php mysql ppt
Karmatechnologies Pvt. Ltd.
 
PPTX
Constructor in java
Pavith Gunasekara
 
PPT
SQLITE Android
Sourabh Sahu
 
PPT
ADO.NET
Wani Zahoor
 
PPTX
Event In JavaScript
ShahDhruv21
 
PPTX
Data Structures in Python
Devashish Kumar
 
PPTX
Chapter 05 classes and objects
Praveen M Jigajinni
 
Introduction to php
shanmukhareddy dasi
 
C# lecture 2: Literals , Variables and Data Types in C#
Dr.Neeraj Kumar Pandey
 
Javascript variables and datatypes
Varun C M
 
Operators in python
eShikshak
 
Java 8 Lambda Expressions
Scott Leberknight
 
Input output files in java
Kavitha713564
 
Generics
Ravi_Kant_Sahu
 
JavaFX Presentation
Mochamad Taufik Mulyadi
 
Android Layout
mcanotes
 
Variables & Data Types In Python | Edureka
Edureka!
 
Classes and objects
Nilesh Dalvi
 
Python Course | Python Programming | Python Tutorial | Python Training | Edureka
Edureka!
 
Constructor in java
Pavith Gunasekara
 
SQLITE Android
Sourabh Sahu
 
ADO.NET
Wani Zahoor
 
Event In JavaScript
ShahDhruv21
 
Data Structures in Python
Devashish Kumar
 
Chapter 05 classes and objects
Praveen M Jigajinni
 

Similar to Python programming workshop session 1 (20)

PPTX
Python basics
RANAALIMAJEEDRAJPUT
 
PDF
Day 1 - Python Overview and Basic Programming - Python Programming Camp - One...
One Year Programming
 
PDF
Chapter 0 Python Overview (Python Programming Lecture)
IoT Code Lab
 
PDF
PART - 1 Python Introduction- Variables- Data types - Numeric- String- Boole...
manikamr074
 
PDF
Sessisgytcfgggggggggggggggggggggggggggggggg
pawankamal3
 
PDF
PYTHON PROGRAMMING NOTES RKREDDY.pdf
Ramakrishna Reddy Bijjam
 
PPT
Python Programming Language
Dr.YNM
 
PDF
Learnpython 151027174258-lva1-app6892
Neeraj Yadav
 
PPT
Python programming
saroja20
 
PDF
Tutorial on-python-programming
Chetan Giridhar
 
PPT
Python Programming Introduction demo.ppt
JohariNawab
 
PDF
web programming UNIT VIII python by Bhavsingh Maloth
Bhavsingh Maloth
 
PDF
20210517-PYTHON AI&DS PROGRAMMING NOTES.pdf
abinayas958164
 
ODP
An Intro to Python in 30 minutes
Sumit Raj
 
PPTX
1-Introduction to Python, features of python, history of python(1).pptx
MAHESWARIS55
 
PPTX
Python ppt
GoogleDeveloperStude2
 
PPT
Introduction to Python Lesson One-Python Easy Learning
johaneskurniawan2
 
PDF
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
KosmikTech1
 
PPTX
UNIT-1 : 20ACS04 – PROBLEM SOLVING AND PROGRAMMING USING PYTHON
Nandakumar P
 
PPT
python-ppt.ppt
MohammadSamiuddin10
 
Python basics
RANAALIMAJEEDRAJPUT
 
Day 1 - Python Overview and Basic Programming - Python Programming Camp - One...
One Year Programming
 
Chapter 0 Python Overview (Python Programming Lecture)
IoT Code Lab
 
PART - 1 Python Introduction- Variables- Data types - Numeric- String- Boole...
manikamr074
 
Sessisgytcfgggggggggggggggggggggggggggggggg
pawankamal3
 
PYTHON PROGRAMMING NOTES RKREDDY.pdf
Ramakrishna Reddy Bijjam
 
Python Programming Language
Dr.YNM
 
Learnpython 151027174258-lva1-app6892
Neeraj Yadav
 
Python programming
saroja20
 
Tutorial on-python-programming
Chetan Giridhar
 
Python Programming Introduction demo.ppt
JohariNawab
 
web programming UNIT VIII python by Bhavsingh Maloth
Bhavsingh Maloth
 
20210517-PYTHON AI&DS PROGRAMMING NOTES.pdf
abinayas958164
 
An Intro to Python in 30 minutes
Sumit Raj
 
1-Introduction to Python, features of python, history of python(1).pptx
MAHESWARIS55
 
Introduction to Python Lesson One-Python Easy Learning
johaneskurniawan2
 
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
KosmikTech1
 
UNIT-1 : 20ACS04 – PROBLEM SOLVING AND PROGRAMMING USING PYTHON
Nandakumar P
 
python-ppt.ppt
MohammadSamiuddin10
 
Ad

More from Abdul Haseeb (9)

PPTX
Python workshop session 6
Abdul Haseeb
 
PPTX
Python programming workshop session 4
Abdul Haseeb
 
PPTX
Python programming workshop session 3
Abdul Haseeb
 
PPTX
Python programming workshop session 2
Abdul Haseeb
 
PPTX
Environmental Friendly Coal Power Plants
Abdul Haseeb
 
PPTX
Low Voltage Circuit Breaker
Abdul Haseeb
 
PPTX
Proteus Circuit Simulation
Abdul Haseeb
 
PPTX
Waste Heat Recovery  System in Cement Plant
Abdul Haseeb
 
PPTX
Environmental Friendly Coal Power Plants
Abdul Haseeb
 
Python workshop session 6
Abdul Haseeb
 
Python programming workshop session 4
Abdul Haseeb
 
Python programming workshop session 3
Abdul Haseeb
 
Python programming workshop session 2
Abdul Haseeb
 
Environmental Friendly Coal Power Plants
Abdul Haseeb
 
Low Voltage Circuit Breaker
Abdul Haseeb
 
Proteus Circuit Simulation
Abdul Haseeb
 
Waste Heat Recovery  System in Cement Plant
Abdul Haseeb
 
Environmental Friendly Coal Power Plants
Abdul Haseeb
 
Ad

Recently uploaded (20)

PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PDF
Free eBook ~100 Common English Proverbs (ebook) pdf.pdf
OH TEIK BIN
 
PPTX
PLANNING A HOSPITAL AND NURSING UNIT.pptx
PRADEEP ABOTHU
 
PDF
TLE 8 QUARTER 1 MODULE WEEK 1 MATATAG CURRICULUM
denniseraya1997
 
PDF
Lesson 1 - Nature of Inquiry and Research.pdf
marvinnbustamante1
 
PPTX
How to Configure Taxes in Company Currency in Odoo 18 Accounting
Celine George
 
PDF
AI-assisted IP-Design lecture from the MIPLM 2025
MIPLM
 
PPTX
Exploring Linear and Angular Quantities and Ergonomic Design.pptx
AngeliqueTolentinoDe
 
PDF
Wikinomics How Mass Collaboration Changes Everything Don Tapscott
wcsqyzf5909
 
PPTX
Natural Language processing using nltk.pptx
Ramakrishna Reddy Bijjam
 
PPTX
Parsing HTML read and write operations and OS Module.pptx
Ramakrishna Reddy Bijjam
 
PPTX
Life and Career Skills Lesson 2.pptxProtective and Risk Factors of Late Adole...
ryangabrielcatalon40
 
PDF
Nanotechnology and Functional Foods Effective Delivery of Bioactive Ingredien...
rmswlwcxai8321
 
PPTX
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
PPTX
grade 8 week 2 ict.pptx. matatag grade 7
VanessaTaberlo
 
PDF
COM and NET Component Services 1st Edition Juval Löwy
kboqcyuw976
 
PDF
Quiz Night Live May 2025 - Intra Pragya Online General Quiz
Pragya - UEM Kolkata Quiz Club
 
PPTX
Marketing Management PPT Unit 1 and Unit 2.pptx
Sri Ramakrishna College of Arts and science
 
PPTX
ENGLISH 8 REVISED K-12 CURRICULUM QUARTER 1 WEEK 1
LeomarrYsraelArzadon
 
PPTX
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
Free eBook ~100 Common English Proverbs (ebook) pdf.pdf
OH TEIK BIN
 
PLANNING A HOSPITAL AND NURSING UNIT.pptx
PRADEEP ABOTHU
 
TLE 8 QUARTER 1 MODULE WEEK 1 MATATAG CURRICULUM
denniseraya1997
 
Lesson 1 - Nature of Inquiry and Research.pdf
marvinnbustamante1
 
How to Configure Taxes in Company Currency in Odoo 18 Accounting
Celine George
 
AI-assisted IP-Design lecture from the MIPLM 2025
MIPLM
 
Exploring Linear and Angular Quantities and Ergonomic Design.pptx
AngeliqueTolentinoDe
 
Wikinomics How Mass Collaboration Changes Everything Don Tapscott
wcsqyzf5909
 
Natural Language processing using nltk.pptx
Ramakrishna Reddy Bijjam
 
Parsing HTML read and write operations and OS Module.pptx
Ramakrishna Reddy Bijjam
 
Life and Career Skills Lesson 2.pptxProtective and Risk Factors of Late Adole...
ryangabrielcatalon40
 
Nanotechnology and Functional Foods Effective Delivery of Bioactive Ingredien...
rmswlwcxai8321
 
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
grade 8 week 2 ict.pptx. matatag grade 7
VanessaTaberlo
 
COM and NET Component Services 1st Edition Juval Löwy
kboqcyuw976
 
Quiz Night Live May 2025 - Intra Pragya Online General Quiz
Pragya - UEM Kolkata Quiz Club
 
Marketing Management PPT Unit 1 and Unit 2.pptx
Sri Ramakrishna College of Arts and science
 
ENGLISH 8 REVISED K-12 CURRICULUM QUARTER 1 WEEK 1
LeomarrYsraelArzadon
 
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 

Python programming workshop session 1

  • 1. Faculty of Engineering Science & Technology(FEST) Hamdard Institute of Engineering Technology(HIET) Hamdard University Instructor ABDUL HASEEB HANDS-ON WORKSHOP ON PYTHON PROGRAMMING LANGUAGE (Day -1) Faculty Development Program (Session-10)
  • 2. The Python Programming Language Audience • Having basic understanding of programming or worked on any High Level language like C, JAVA, or C ++, etc At the end of today`s session you will be able to: • Understand the significance of Python Programming • Familiarize with the Python IDE Environment • Understand the Input and Output operations in Python • Use variables in Python language
  • 3. What is Python Programming • Python was developed by Guido van Rossum (a Dutch programmer) in 1991 • Python is a High Level Language (whereas C and Java are not High Level Language) • Python has a design philosophy that emphasizes Code Readability • The syntax allows programmer to express concepts in fewer lines of code than might be used in languages such as C++, Java etc
  • 4. Why Python Programming According to IEEE Spectrum the top Programming Languages in 2017 are: http://spectrum.ieee.org/computing/software/the-2017-top-programming-languages
  • 5. Because it Easy to learn and use: Its simple syntax is very accessible to programming novices and will look familiar to anyone with experience in Matlab, C/C++, Java. Also Python has powerful libraries Why Python Programming
  • 6. • Because it is FREE There is no worried of buying the key and or unethical use of software by cracking its key • Because it is Open Source Why Python Programming In the Open Source software the source code, blue print and documentation are freely available to public. The software developer voluntarily put their part to add the features in the software
  • 7. Python has vast development implementation
  • 8. Python Cross-compilers to other languages
  • 10. Python in other Science
  • 11. Python has Powerful Libraries
  • 13. How to Learn Python
  • 14. Working on Python IDLE Download Python IDLE 2.x or 3.x from Python Software Foundation Website for Free
  • 15. Python IDLE Environment IDLE (Integrated Development Learning Environment) • IDLE has multi-window text editor with syntax highlighting, auto-completion, smart indent • The Shell window execute command at the statement complete • User code written in Text Editor window
  • 16. Some Useful command for Python IDLE Shell window • Alt + p to retrieve Previous Command • Alt + n to retrieve Next Command • To get help of instruction or command use help(instruction) Python IDLE Environment
  • 17. My First Python Program Open Python IDLE Shell window and write the following instruction A print is an output instruction use to display string or data value on screen print("Hamdard University") Note: Program contain no header file, no main() function, no semi column at instruction end, even you write string in single quotation mark. BUT one thing common with other programming language that is Case Sensitive print('Hamdard University‘)
  • 18. Output Instruction Escape Sequence Working Backslash () ' Single quote (') " Double quote (") n ASCII Linefeed (LF) t ASCII Horizontal Tab (TAB) Example: >>>print('BackslashtTabnNewline') Backslash Tab Newline` The following are the Escape Sequence use with print instruction
  • 19. Python Data types Python has five standard data types • Numbers(Integer and Float) • String • List (Array) • Tuple (Constant Array) • Dictionary Variable Number type: • Integer (whole number) • Float (decimal point number) • Character/String (ASCII format) • Boolean (True and False)
  • 20. Python Variable • Type of Variable: Check the type of Variable • Deleting Variable: Delete the declare variable Syntax: del(variable_name) >>> a = 2 >>> print(a) 2 >>> del(a) >>> print(a) NameError: name 'a' is not defined Variable value Type command Result a=2 type(a) <class 'int'> b=3.5 type(b) <class 'float'> c=’f’ type(c) <class 'str'> d=’abc123’ type(d) <class 'str'> type(True) <class 'bool'> type(False) <class 'bool'>
  • 21. Python is an Interpreter Language Working Compiler Interpreter Converting code Convert whole code Step by step Translation time Slow Fast Execution time Fast Slow Programming Languages C, Visual Basic, Java, LabView etc Python, Matlab, PHP, MS Excel etc
  • 22. Python Interpreter Example In Interpreter, if code contains error it may run partially and stop when error occurs. In the following code Error is present in line number 7 that is “d=B” where ‘B’ is not define before, so program runs and execute up to that line.
  • 23. Input Instruction • Input Instruction: To take an input from user through keyboard input() command is used, this instruction take input from keyboard in the form of ASCII character and store it the variable. • Syntax: take_input = input(1 Argument) • Example: name = input('Enter your name =') age = input('Enter your age = ') print('Your name is ', name , ',and your age is = ',age)
  • 24. Python Version • Python Version 1 is obsolete now • Python Version 2 and Python Version 3 is active • The Python 2 is old version but retain for programming because lots of module was developed on it which not work with Python 3 • There is only some difference in execution of instructions other wise both have same working
  • 25. Difference between Python 2 and Python 3 Output Instruction Python 2 Python 3 Python 2 allows to write the print instruction argument with in Round bracket ( and) or with out it. The argument of the print instruction must written with in Round bracket ( and) >>> print('String') #Allow String >>> print("String") #Allow String >>> print 'String' #Allow String >>> print "String" #Allow String >>> print('String') #Allow String >>> print("String") #Allow String >>> print 'String' #NotAllow SyntaxError: Missing parentheses in call to 'print‘ >>> print "String" #NotAllow SyntaxError: Missing parentheses in call to 'print‘
  • 26. Difference between Python 2 and Python 3 Input Instruction Python 2 Python 3 input(prompt) raw_input(prompt) Input(prompt) It keeps the type of user enter data Automatically update the Input variable to the user enter data Always take data in the string format regardless of the user enter data Syntax: Input_variable = input(prompt) Input_variable data type is same as user enter data Syntax: Input_variable = input(prompt) Input_variable is String Syntax: Input_variable = input(prompt) Input_variable is String
  • 27. Input Instruction Example Python 2 Python 3 input(prompt) raw_input(prompt) Input(prompt) >>> a = input("Enter = ") Enter = 12 >>> type(a) <type 'int'> >>> a = input("Enter = ") Enter = 12.12 >>> type(a) <type 'float'> >>> a = input("Enter = ") Enter = "Ab12" >>> type(a) <type 'str'> >>> a = input("Enter = ") Enter = Ab12 NameError: name 'Ab12' is not defined >>> a = raw_input("Enter = ") Enter = 12 >>> type(a) <type 'str'> >>> a = raw_input("Enter = ") Enter = 12.12 >>> type(a) <type 'str'> >>> a = raw_input("Enter = ") Enter = "Ab12" >>> type(a) <type 'str'> >>> a = raw_input("Enter = ") Enter = Ab12 >>> type(a) <type 'str'> >>> a = input("Enter = ") Enter = 12 >>> type(a) <class 'str'> >>> a = input("Enter = ") Enter = 12.12 >>> type(a) <class 'str'> >>> a = input("Enter = ") Enter = "Ab12" >>> type(a) <class 'str'> >>> a = input("Enter = ") Enter = Ab12 >>> type(a) <class 'str'> Difference between Python 2 and Python 3
  • 28. Difference between Python 2 and Python 3 (problem and its solution) Python 2 Python 3 Program num = input('Enter a number = ') print num,‘x 3 = ',num * 3 num = input('Enter a number = ') print( num,‘x 3 = ',num * 3 ) Output Enter a number = 5 5 x 3 = 15 Enter a number = 5 5 x 3 = 555 Observation: the num is integer so number 5 x 3 is number 15 Observation: the num is String so String 5 x 3 is three times of number 5 The problem in the output of Python 3 is solved in later slides
  • 29. Change the variable type Function Description Example Instruction Output str(x) Converts object x to a string representation. str(75) str("25.25") ‘75’ '25.25' int(x) Converts object x to a integer number. int(35.26) int(100.101 ) 35 100 float(x) Converts object x to a float number. float(35) float(100) 35.0 100.0 list(s) Converts s to a list. list(range(5,10)) list(range(1,10,2)) [5, 6, 7, 8, 9] [1, 3, 5, 7, 9] tuple(s) Converts s to a tuple. tuple(range(5,10)) tuple(range(1,10,2)) (5, 6, 7, 8, 9) (1, 3, 5, 7, 9) dict(d) Creates a dictionary. d must be a sequence of (key,value) tuples. chr(x) Converts an integer to a character. chr(65) chr(97) 'A‘ ‘a’ ord(x) Converts a single character to its ASCII (integer value) ord('A') ord(‘a') 65 97 hex(x) Converts an integer to a hexadecimal string. hex(16) hex(100) '0x10‘ '0x64' oct(x) Converts an integer to an octal string. oct(9) oct(50) '0o11‘ '0o62'
  • 30. Exercise Rewrite the following program in Python 3 with correct output display Without changing Variable Type Changing Variable Type num = input(“Enter a number = “) print( num,”x 3 = “,num * 3 ) num = input(“Enter a number = “) print( num,“x 3 = ",int(num) * 3 ) 3 x 3 = 333 3 x 3 = 9
  • 31. Python IDEs • Python IDLE • PyCharm • Sublime Text • Atom • Geany • Anjuta • Eric • Komodo IDE • KDevelop • Ninja-IDE • • Spyder
  • 32. Integer Operation Function Description bit_length(...) Number of bits necessary to represent self in binary Example: >>> (37).bit_length() 6 >>> int_num.bit_length() 2 bin(…) Return binary of the integer Example: >>> bin(37) '0b100101' >>> bin(25) '0b1111111' Denominator The denominator of a rational number in lowest terms Imag The imaginary part of a complex number numerator The numerator of a rational number in lowest terms real The real part of a complex number conjugate(...) Returns self, the complex conjugate of any int. to_bytes(...) Return an array of bytes representing an integer.
  • 33. Float Operation Function Description float.fromhex (...) Create a floating-point number from a hexadecimal string. Example: >>>float.fromhex('0x1.ffffp10') 2047.984375 float.hex(…) Return a hexadecimal representation of a floating-point number Example: >>>3.14159.hex() '0x1.921f9f01b866ep+1' is_integer(...) Return True if the float is an integer Example: >>> num = 5.5 >>> num.is_integer() False >>> num = num - 0.5 # remove floating value >>> num.is_integer() True as_integer_ratio(...) Return a pair of integers, whose ratio is exactly equal to the original float and with a positive denominator. Example: >>> (10.0).as_integer_ratio() (10, 1) >>> (0.0).as_integer_ratio() (0, 1) >>> (-.25).as_integer_ratio() (-1, 4) conjugate(...) Return self, the complex conjugate of any float. imag The imaginary part of a complex number Real The real part of a complex number
  • 34. String Operation Function Description len(string) Return length of string capitalize(...) Return a capitalized version of S, i.e. make the first character have upper case and the rest lower case. title(...) Return a title cased version of S, i.e. words start with title case characters, and all remaining cased characters have lower case. lower(...) Return a copy of the string S converted to lowercase. upper(...) Return a copy of S converted to uppercase. casefold(...) Return a version of S suitable for caseless comparisons islower(...) Return True if all cased characters in S are lowercase and there is at least one cased character in S, False otherwise. isupper(...) Return True if all cased characters in S are uppercase and there is at least one cased character in S, False otherwise. isnumeric(...) Return True if there are only numeric characters in S, False otherwise. isdecimal(...) Return True if there are only decimal characters in S, False otherwise isdigit(...) Return True if all characters in S are digits and there is at least one character in S, False otherwise.
  • 35. a = input("Enter your name:") print("Lower Case of Enter string = " ,a.lower()) print("Upper Case of Enter string = " , a.upper()) print("Title Case of Enter string = " , a.title()) print("Length of Enter string = " , len(a)) print("Your Enter string is Alpha = " , a.isalpha()) print("Your Enter string is Decimal = " , a.isdecimal()) print("Your Enter string is Digit = " , a.isdigit()) print("Your Enter string is in Lower case = " , a.islower()) print("Your Enter string is in Upper case = " , a.isupper()) print("Your Enter string is in Title case = " , a.istitle()) Output 1: Enter your name:Hamdard university Lower Case of Enter string = hamdard university Upper Case of Enter string = HAMDARD UNIVERSITY Title Case of Enter string = Hamdard University Length of Enter string = 18 Your Enter string is Alpha = False Your Enter string is Decimal = False Your Enter string is Digit = False Your Enter string is in Lower case = False Your Enter string is in Upper case = False Your Enter string is in Title case = False Output 2: Enter your name:123456 Lower Case of Enter string = 123456 Upper Case of Enter string = 123456 Title Case of Enter string = 123456 Length of Enter string = 6 Your Enter string is Alpha = False Your Enter string is Decimal = True Your Enter string is Digit = True Your Enter string is in Lower case = False Your Enter string is in Upper case = False Your Enter string is in Title case = False String Operation Example
  • 36. Comparing Python with C/C++ Programming Parameter Python Language C/C++ Language Programming type Interpreter Compiler Programming Language High level Language Middle level Language Header file import #include Code block Whitespace Indentation Code within curly bracket { code} Single line statement termination Nothing Semicolon ; Control flow (Multi line) statement termination Colon : Nothing Single line comments # comments //comments Multi-line comments ‘’’ Comments lines ‘’’ /* Comments lines */ If error code found Partial run code until find error Did not run until error is present Program length Take fewer lines Take relatively more lines