SlideShare a Scribd company logo
Introduction to
Python programming
Submitted by ASTHA CHAURASIA Submitted to Prof.Mir Shahnawaz Ahmad
Variables
▶ Binding between a name and an object
▶ Single variable assignment: x = 1
▶ Multi variable assignment: x, y = 1, 2
▶ Swap values: x, y = y, x
Data Types
▶ Numbers: int (Integers), float (Real Numbers), bool (Boolean, a subset of int)
▶ Immutable Types: str (string), tuple, bytes, frozenset
▶ Mutable Types: list, set, bytearray, dict (dictionary)
▶ Sequence Types: str, tuple, bytes, bytearray, list
▶ Determining the type of an object: type()
Numbers: int and float
▶ 1 + 2 (addition)
▶ 1 – 2 (subtraction)
▶ 1 * 2 (multiplication)
▶ 1 / 2 (division)
▶ 1 // 2 (integer or floor division)
▶ 3 % 2 (modulus or remainder of the division)
▶ 2**2 (power)
Numbers: bool (continuation)
▶ 1 > 2
▶ 1 < 2
▶ 1 == 2
▶ Boolean operations: and, or, not
▶ Objects can also be tested for their truth value. The following values are false:
None, False, zero of any numeric type, empty sequences, empty mapping
str (String)
▶ x = “This is a string”
▶ x = ‘This is also a string’
▶ x = “””So is this one”””
▶ x = ‘’’And this one as well’’’
▶ x = “””
This is a string that spans more
than one line. This can also be used
for comments.
“””
str (continuation)
▶ Indexing elements: x[0] is the first element, x[1] is the second, and so on
▶ Slicing:
▶ [start:end:step]
▶ [start:] # end is the length of the sequence, step assumed to be 1
▶ [:end] # start is the beginning of the sequence, step assumed to be 1
▶ [::step] # start is the beginning of the sequence, end is the length
▶ [start::step]
▶ [:end:step]
▶ These operations are common for all sequence types
str (continuation)
▶ Some common string methods:
▶ join (concatenates the strings from an iterable using the string as glue)
▶ format (returns a formatted version of the string)
▶ strip (returns a copy of the string without leading and trailing whitespace)
▶ Use help(str.<command>) in the interactive shell and dir(str)
Control Flow (pt. 1): if statement
▶ Compound statement
if <expression>:
suite
elif <expression2>:
suite
else:
suite
Control Flow (pt. 2): if statement
age = int(input(“> “))
if age >= 30:
print(“You are 30 or above”)
elif 20 < age < 30:
print(“You are in your twenties”)
else:
print(“You are less than 20”)
list
▶ x = [] # empty list
▶ x = [1, 2, 3] # list with 3 elements
▶ x = list(“Hello”)
▶ x.append(“something”) # append object to the end of the list
▶ x.insert(2, “something”) # append object before index 2
dict (Dictionaries)
▶ Mapping between keys and values
▶ Values can be of whatever type
▶ Keys must be hashable
▶ x = {} # empty dictionary
▶ x = {“Name”: “John”, “Age”: 23}
▶ x.keys()
▶ x.values()
▶ x.items()
Control Flow: for loop
▶ Also compound statement
▶ Iterates over the elements of an iterable object
for <target> in <expression>:
suite
else:
suite
Control Flow: for loop (continuation)
colors = [“red”, “green”, “blue”, “orange”]
for color in colors:
print(color)
colors = [[1, “red”], [2, “green”], [3, “blue”], [4, “orange”]]
for i, color in colors:
print(i, “ ---> “, color)
Control Flow: for loop (continuation)
▶ Iterable is a container object able to return its elements one at a time
▶ Iterables use iterators to return their elements one at a time
▶ Iterator is an object that represents a stream of data
▶ Must implement two methods: iter and next (Iterator protocol)
▶ Raises StopIteration when elements are exhausted
▶ Lazy evaluation
Challenge
▶ Rewrite the following code using enumerate and the following list of colors:
[“red”, “green”, “blue”, “orange”] .
(hint: help(enumerate))
colors = [[1, “red”], [2, “green”], [3, “blue”], [4, “orange”]]
for i, color in colors:
print(i, “ ---> “, color)
Control Flow: for loop (continuation)
▶ range: represents a sequence of integers
▶ range(stop)
▶ range(start, stop)
▶ range(start, stop, step)
Control Flow: for loop (continuation)
colors = [“red”, “green”, “orange”, “blue”]
for color in colors:
print(color)
else:
print(“Done!”)
Control Flow: while loop
▶ Executes the suite of statements as long as the expression evaluates to True
while <expression>:
suite
else:
suite
Control Flow: while loop (continuation)
counter = 5
while counter > 0:
print(counter)
counter = counter - 1
counter = 5
while counter > 0:
print(counter)
counter = counter – 1
else:
print(“Done!”)
Challenge
▶ Rewrite the following code using a for loop and range:
counter = 5
while counter > 0:
print(counter)
counter = counter - 1
Control Flow: break and continue
▶ Can only occur nested in a for or while loop
▶ Change the normal flow of execution of a loop:
▶ break stops the loop
▶ continue skips to the next iteration
for i in range(10):
if i % 2 == 0:
continue
else:
print(i)
Control Flow: break and (continue)
colors = [“red”, “green”, “blue”, “purple”, “orange”]
for color in colors:
if len(color) > 5:
break
else:
print(color)
Challenge
▶ Rewrite the following code without the if statement (hint: use the step in range)
for i in range(10):
if i % 2 == 0:
continue
else:
print(i)
Reading material
▶ Data Model (Python Language Reference):
https://docs.python.org/3/reference/datamodel.html
▶ Theif statement (Python Language Reference):
https://docs.python.org/3/reference/compound_stmts.html#the-if-statement
▶ Thefor statement (Python Language Reference):
https://docs.python.org/3/reference/compound_stmts.html#the-for-statement
▶ Thewhile statement (Python Language Reference):
https://docs.python.org/3/reference/compound_stmts.html#the-while-statement
More resources
▶ Python Tutorial: https://docs.python.org/3/tutorial/index.html
▶ Python Language Reference: https://docs.python.org/3/reference/index.html
▶ Slack channel: https://startcareerpython.slack.com/
▶ Start a Career with Python newsletter: https://www.startacareerwithpython.com/
▶ Book 15% off (NZ6SZFBL): https://www.createspace.com/6506874
set
▶ Unordered mutable collection of elements
▶ Doesn’t allow duplicate elements
▶ Elements must be hashable
▶ Useful to test membership
▶ x = set() # empty set
▶ x = {1, 2, 3} # set with 3 integers
▶ 2 in x # membership test
tuple
▶ x = 1,
▶ x = (1,)
▶ x = 1, 2, 3
▶ x = (1, 2, 3)
▶ x = (1, “Hello, world!”)
▶ You can also slice tuples
bytes
▶ Immutable sequence of bytes
▶ Each element is an ASCII character
▶ Integers greater than 127 must be properly escaped
▶ x = b”This is a bytes object”
▶ x = b’This is also a bytes object’
▶ x = b”””So is this”””
▶ x = b’’’or even this’’’
bytearray
▶ Mutable counterpart of bytes
▶ x = bytearray()
▶ x = bytearray(10)
▶ x = bytearray(b”Hello, world!”)

More Related Content

Similar to introductionpart1-160906115340 (1).pptx

Python for Scientific Computing
Python for Scientific ComputingPython for Scientific Computing
Python for Scientific Computing
Albert DeFusco
 
Python cheatsheat.pdf
Python cheatsheat.pdfPython cheatsheat.pdf
Python cheatsheat.pdf
HimoZZZ
 
Day2
Day2Day2
PPt Revision of the basics of python1.pptx
PPt Revision of the basics of python1.pptxPPt Revision of the basics of python1.pptx
PPt Revision of the basics of python1.pptx
tcsonline1222
 
MODULE. .pptx
MODULE.                              .pptxMODULE.                              .pptx
MODULE. .pptx
Alpha337901
 
Python for Beginners(v1)
Python for Beginners(v1)Python for Beginners(v1)
Python for Beginners(v1)
Panimalar Engineering College
 
Python Workshop. LUG Maniapl
Python Workshop. LUG ManiaplPython Workshop. LUG Maniapl
Python Workshop. LUG Maniapl
Ankur Shrivastava
 
Python-CH01L04-Presentation.pptx
Python-CH01L04-Presentation.pptxPython-CH01L04-Presentation.pptx
Python-CH01L04-Presentation.pptx
ElijahSantos4
 
Loops in Python.pptx
Loops in Python.pptxLoops in Python.pptx
Introduction to python programming 1
Introduction to python programming   1Introduction to python programming   1
Introduction to python programming 1
Giovanni Della Lunga
 
Python 3.pptx
Python 3.pptxPython 3.pptx
Python 3.pptx
HarishParthasarathy4
 
Porting to Python 3
Porting to Python 3Porting to Python 3
Porting to Python 3
Lennart Regebro
 
Python Session - 3
Python Session - 3Python Session - 3
Python Session - 3
AnirudhaGaikwad4
 
Declare Your Language: Name Resolution
Declare Your Language: Name ResolutionDeclare Your Language: Name Resolution
Declare Your Language: Name Resolution
Eelco Visser
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
Utkarsh Sengar
 
Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)
Pedro Rodrigues
 
Data Handling.pdf
Data Handling.pdfData Handling.pdf
Data Handling.pdf
MILANOP1
 
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptxPython Programming for basic beginners.pptx
Python Programming for basic beginners.pptx
mohitesoham12
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
Chu An
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
Rakotoarison Louis Frederick
 

Similar to introductionpart1-160906115340 (1).pptx (20)

Python for Scientific Computing
Python for Scientific ComputingPython for Scientific Computing
Python for Scientific Computing
 
Python cheatsheat.pdf
Python cheatsheat.pdfPython cheatsheat.pdf
Python cheatsheat.pdf
 
Day2
Day2Day2
Day2
 
PPt Revision of the basics of python1.pptx
PPt Revision of the basics of python1.pptxPPt Revision of the basics of python1.pptx
PPt Revision of the basics of python1.pptx
 
MODULE. .pptx
MODULE.                              .pptxMODULE.                              .pptx
MODULE. .pptx
 
Python for Beginners(v1)
Python for Beginners(v1)Python for Beginners(v1)
Python for Beginners(v1)
 
Python Workshop. LUG Maniapl
Python Workshop. LUG ManiaplPython Workshop. LUG Maniapl
Python Workshop. LUG Maniapl
 
Python-CH01L04-Presentation.pptx
Python-CH01L04-Presentation.pptxPython-CH01L04-Presentation.pptx
Python-CH01L04-Presentation.pptx
 
Loops in Python.pptx
Loops in Python.pptxLoops in Python.pptx
Loops in Python.pptx
 
Introduction to python programming 1
Introduction to python programming   1Introduction to python programming   1
Introduction to python programming 1
 
Python 3.pptx
Python 3.pptxPython 3.pptx
Python 3.pptx
 
Porting to Python 3
Porting to Python 3Porting to Python 3
Porting to Python 3
 
Python Session - 3
Python Session - 3Python Session - 3
Python Session - 3
 
Declare Your Language: Name Resolution
Declare Your Language: Name ResolutionDeclare Your Language: Name Resolution
Declare Your Language: Name Resolution
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)
 
Data Handling.pdf
Data Handling.pdfData Handling.pdf
Data Handling.pdf
 
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptxPython Programming for basic beginners.pptx
Python Programming for basic beginners.pptx
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 

More from AsthaChaurasia4

SENSORS.pptx
SENSORS.pptxSENSORS.pptx
SENSORS.pptx
AsthaChaurasia4
 
datastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptxdatastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptx
AsthaChaurasia4
 
PROFICIENCY PRESENTATION.pptx
PROFICIENCY PRESENTATION.pptxPROFICIENCY PRESENTATION.pptx
PROFICIENCY PRESENTATION.pptx
AsthaChaurasia4
 
beeeppt.pptx
beeeppt.pptxbeeeppt.pptx
beeeppt.pptx
AsthaChaurasia4
 
numbersystem-211022083557.pdf
numbersystem-211022083557.pdfnumbersystem-211022083557.pdf
numbersystem-211022083557.pdf
AsthaChaurasia4
 
lecture02-numbersystem-191002152647.pdf
lecture02-numbersystem-191002152647.pdflecture02-numbersystem-191002152647.pdf
lecture02-numbersystem-191002152647.pdf
AsthaChaurasia4
 
BEEEE PROJECT.pptx
BEEEE PROJECT.pptxBEEEE PROJECT.pptx
BEEEE PROJECT.pptx
AsthaChaurasia4
 
beee.pptx
beee.pptxbeee.pptx
beee.pptx
AsthaChaurasia4
 

More from AsthaChaurasia4 (9)

SENSORS.pptx
SENSORS.pptxSENSORS.pptx
SENSORS.pptx
 
datastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptxdatastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptx
 
PROFICIENCY PRESENTATION.pptx
PROFICIENCY PRESENTATION.pptxPROFICIENCY PRESENTATION.pptx
PROFICIENCY PRESENTATION.pptx
 
beeeppt.pptx
beeeppt.pptxbeeeppt.pptx
beeeppt.pptx
 
numbersystem-211022083557.pdf
numbersystem-211022083557.pdfnumbersystem-211022083557.pdf
numbersystem-211022083557.pdf
 
lecture02-numbersystem-191002152647.pdf
lecture02-numbersystem-191002152647.pdflecture02-numbersystem-191002152647.pdf
lecture02-numbersystem-191002152647.pdf
 
BEEEE PROJECT.pptx
BEEEE PROJECT.pptxBEEEE PROJECT.pptx
BEEEE PROJECT.pptx
 
AIML.pptx
AIML.pptxAIML.pptx
AIML.pptx
 
beee.pptx
beee.pptxbeee.pptx
beee.pptx
 

Recently uploaded

Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 

Recently uploaded (20)

Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 

introductionpart1-160906115340 (1).pptx

  • 1. Introduction to Python programming Submitted by ASTHA CHAURASIA Submitted to Prof.Mir Shahnawaz Ahmad
  • 2. Variables ▶ Binding between a name and an object ▶ Single variable assignment: x = 1 ▶ Multi variable assignment: x, y = 1, 2 ▶ Swap values: x, y = y, x
  • 3. Data Types ▶ Numbers: int (Integers), float (Real Numbers), bool (Boolean, a subset of int) ▶ Immutable Types: str (string), tuple, bytes, frozenset ▶ Mutable Types: list, set, bytearray, dict (dictionary) ▶ Sequence Types: str, tuple, bytes, bytearray, list ▶ Determining the type of an object: type()
  • 4. Numbers: int and float ▶ 1 + 2 (addition) ▶ 1 – 2 (subtraction) ▶ 1 * 2 (multiplication) ▶ 1 / 2 (division) ▶ 1 // 2 (integer or floor division) ▶ 3 % 2 (modulus or remainder of the division) ▶ 2**2 (power)
  • 5. Numbers: bool (continuation) ▶ 1 > 2 ▶ 1 < 2 ▶ 1 == 2 ▶ Boolean operations: and, or, not ▶ Objects can also be tested for their truth value. The following values are false: None, False, zero of any numeric type, empty sequences, empty mapping
  • 6. str (String) ▶ x = “This is a string” ▶ x = ‘This is also a string’ ▶ x = “””So is this one””” ▶ x = ‘’’And this one as well’’’ ▶ x = “”” This is a string that spans more than one line. This can also be used for comments. “””
  • 7. str (continuation) ▶ Indexing elements: x[0] is the first element, x[1] is the second, and so on ▶ Slicing: ▶ [start:end:step] ▶ [start:] # end is the length of the sequence, step assumed to be 1 ▶ [:end] # start is the beginning of the sequence, step assumed to be 1 ▶ [::step] # start is the beginning of the sequence, end is the length ▶ [start::step] ▶ [:end:step] ▶ These operations are common for all sequence types
  • 8. str (continuation) ▶ Some common string methods: ▶ join (concatenates the strings from an iterable using the string as glue) ▶ format (returns a formatted version of the string) ▶ strip (returns a copy of the string without leading and trailing whitespace) ▶ Use help(str.<command>) in the interactive shell and dir(str)
  • 9. Control Flow (pt. 1): if statement ▶ Compound statement if <expression>: suite elif <expression2>: suite else: suite
  • 10. Control Flow (pt. 2): if statement age = int(input(“> “)) if age >= 30: print(“You are 30 or above”) elif 20 < age < 30: print(“You are in your twenties”) else: print(“You are less than 20”)
  • 11. list ▶ x = [] # empty list ▶ x = [1, 2, 3] # list with 3 elements ▶ x = list(“Hello”) ▶ x.append(“something”) # append object to the end of the list ▶ x.insert(2, “something”) # append object before index 2
  • 12. dict (Dictionaries) ▶ Mapping between keys and values ▶ Values can be of whatever type ▶ Keys must be hashable ▶ x = {} # empty dictionary ▶ x = {“Name”: “John”, “Age”: 23} ▶ x.keys() ▶ x.values() ▶ x.items()
  • 13. Control Flow: for loop ▶ Also compound statement ▶ Iterates over the elements of an iterable object for <target> in <expression>: suite else: suite
  • 14. Control Flow: for loop (continuation) colors = [“red”, “green”, “blue”, “orange”] for color in colors: print(color) colors = [[1, “red”], [2, “green”], [3, “blue”], [4, “orange”]] for i, color in colors: print(i, “ ---> “, color)
  • 15. Control Flow: for loop (continuation) ▶ Iterable is a container object able to return its elements one at a time ▶ Iterables use iterators to return their elements one at a time ▶ Iterator is an object that represents a stream of data ▶ Must implement two methods: iter and next (Iterator protocol) ▶ Raises StopIteration when elements are exhausted ▶ Lazy evaluation
  • 16. Challenge ▶ Rewrite the following code using enumerate and the following list of colors: [“red”, “green”, “blue”, “orange”] . (hint: help(enumerate)) colors = [[1, “red”], [2, “green”], [3, “blue”], [4, “orange”]] for i, color in colors: print(i, “ ---> “, color)
  • 17. Control Flow: for loop (continuation) ▶ range: represents a sequence of integers ▶ range(stop) ▶ range(start, stop) ▶ range(start, stop, step)
  • 18. Control Flow: for loop (continuation) colors = [“red”, “green”, “orange”, “blue”] for color in colors: print(color) else: print(“Done!”)
  • 19. Control Flow: while loop ▶ Executes the suite of statements as long as the expression evaluates to True while <expression>: suite else: suite
  • 20. Control Flow: while loop (continuation) counter = 5 while counter > 0: print(counter) counter = counter - 1 counter = 5 while counter > 0: print(counter) counter = counter – 1 else: print(“Done!”)
  • 21. Challenge ▶ Rewrite the following code using a for loop and range: counter = 5 while counter > 0: print(counter) counter = counter - 1
  • 22. Control Flow: break and continue ▶ Can only occur nested in a for or while loop ▶ Change the normal flow of execution of a loop: ▶ break stops the loop ▶ continue skips to the next iteration for i in range(10): if i % 2 == 0: continue else: print(i)
  • 23. Control Flow: break and (continue) colors = [“red”, “green”, “blue”, “purple”, “orange”] for color in colors: if len(color) > 5: break else: print(color)
  • 24. Challenge ▶ Rewrite the following code without the if statement (hint: use the step in range) for i in range(10): if i % 2 == 0: continue else: print(i)
  • 25. Reading material ▶ Data Model (Python Language Reference): https://docs.python.org/3/reference/datamodel.html ▶ Theif statement (Python Language Reference): https://docs.python.org/3/reference/compound_stmts.html#the-if-statement ▶ Thefor statement (Python Language Reference): https://docs.python.org/3/reference/compound_stmts.html#the-for-statement ▶ Thewhile statement (Python Language Reference): https://docs.python.org/3/reference/compound_stmts.html#the-while-statement
  • 26. More resources ▶ Python Tutorial: https://docs.python.org/3/tutorial/index.html ▶ Python Language Reference: https://docs.python.org/3/reference/index.html ▶ Slack channel: https://startcareerpython.slack.com/ ▶ Start a Career with Python newsletter: https://www.startacareerwithpython.com/ ▶ Book 15% off (NZ6SZFBL): https://www.createspace.com/6506874
  • 27. set ▶ Unordered mutable collection of elements ▶ Doesn’t allow duplicate elements ▶ Elements must be hashable ▶ Useful to test membership ▶ x = set() # empty set ▶ x = {1, 2, 3} # set with 3 integers ▶ 2 in x # membership test
  • 28. tuple ▶ x = 1, ▶ x = (1,) ▶ x = 1, 2, 3 ▶ x = (1, 2, 3) ▶ x = (1, “Hello, world!”) ▶ You can also slice tuples
  • 29. bytes ▶ Immutable sequence of bytes ▶ Each element is an ASCII character ▶ Integers greater than 127 must be properly escaped ▶ x = b”This is a bytes object” ▶ x = b’This is also a bytes object’ ▶ x = b”””So is this””” ▶ x = b’’’or even this’’’
  • 30. bytearray ▶ Mutable counterpart of bytes ▶ x = bytearray() ▶ x = bytearray(10) ▶ x = bytearray(b”Hello, world!”)