SlideShare a Scribd company logo
1 of 23
Python 2
Basic Python Part 1
Izyan Yasmin 2017
Requirements
 Python editor:
 Spyder (Anaconda )
 Python version: 2.7 and above
Izyan Yasmin 2017
1.0 Python numeric types
 Basic operation ( + , - , * , / )
To get the accurate
answer, write in a
decimal point.
Because number
without decimal
point is consider as
integer, a whole
number.
Two same function, but
different approach. Print
function will round-off
the nearest number.
Izyan Yasmin 2017
1.1 Assignment statement
 Name can be attach to a value and the value will stay around for the rest of
conversational Python session.
 Python names must start with a letter or the underbar(_)character.
 Examples:
 How many seconds in 1 days?
Izyan Yasmin 2017
 General form of an assignments
 𝑛𝑎𝑚𝑒1 = 𝑛𝑎𝑚𝑒2 = ⋯ = 𝑒𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛
It gives the same
output, since it
carries the same
expression.
Izyan Yasmin 2017
2.0 Character String Basics
 In python you can choose to enclose strings either using single-quotes (‘…’)
or double-quotes (“……”)
Izyan Yasmin 2017
Print function
will output
the string
without the
quotes.
 Converting string, 𝑠𝑡𝑟 to integer, 𝑖𝑛𝑡 or vice-versa
Izyan Yasmin 2017
 Python has several escape sequences.
Izyan Yasmin 2017
If a line is not
finished,
python will
displays a
“….” prompt. Using print
function, will
gives the
exact output
in 3 lines.
It will read
directly the line
and output using
the special
escape character
n
2.1 indexing strings
 To extract one or more characters from a string value, you need to know the
position. The diagram shows the position between characters, which its
starts before the first to the end of strings.
Izyan Yasmin 2017
2.2 String methods
Method General expression Examples
center()
To center text
s.center(n)
.ljust() and .rjust()
Pad to length on left or
right
s.ljust(n)
s.rjust(n)
.strip(), .lstrip() and
.rstrip()
Remove leading and/or
trailing whitespace
s.strip()
s.lstrip()
s.rstrip()
count()
Search string s to see
how many times t occur
in string s
s.count(t)
Izyan Yasmin 2017
Many expression on strings are expressed as method. To call a method, we can use this syntax:
• expr.method(𝑎𝑟𝑔1, 𝑎𝑟𝑔2, … )
If length isn’t enough, it
will return the original
values.
Izyan Yasmin 2017
Method General expression Examples
.find() and r.find()
Locate string within a
longer string
s.find(t)
.startswith() and
.endswith()
To check if a string s
starts with a string t
s.startswith(t)
s.endswith(t)
.lower() and .upper()
Used to convert
uppercased characters to
lower and vice-versa
s.lower()
s.uppper()
.split()
Break field out of a
strings
s.split()
2.3 String format method
 To combine string operations is to combine fixed text and variable values
into single string.
 General string of format operations:
 s.format(𝑝0, 𝑝1, … , 𝑘0 = 𝑒 𝑜, 𝑘1 = 𝑒1)
 Formatting of an item can be control using a format code of the form “{N:type}”, N is
number of arguments to the . 𝑓𝑜𝑟𝑚𝑎𝑡() method, and type specifies details of the
formatting.
Izyan Yasmin 2017
3.0 Sequence types
 A sequence in Python represents an ordered set.
 str and unicode used to hold text, that is strings characters.
 list and tuple used for sequence of zero or more values of any type.
 Use list if the content of the sequence may change. Enclosed with square bracket “[…]”.
[𝑒𝑥𝑝𝑟1, 𝑒𝑥𝑝𝑟2, … . ]
 Use tuple if the contents may not change. Enclosed in parentheses “(….)”
(𝑒𝑥𝑝𝑟1, 𝑒𝑥𝑝𝑟2, … . )
Izyan Yasmin 2017
3.1 Function and operators
Description Operators Example
Return numbers of elements
in a sequence s
len(s)
Return largest value in a
sequence s
max(s)
Return smallest value in
sequence s
min(s)
To test set of membership in
To concatenate two sequence
of the same type
+
To get a new sequence
containing n repetition of
elements of s
*
Izyan Yasmin 2017
3.2 Indexing the position in a sequence
 Positions in a sequence refer to locations between the values, using an
expression s[i].
Izyan Yasmin 2017
Last line is an error because there is
nothing in position 12 in string fruits.
3.3 Slicing sequences
Izyan Yasmin 2017
Types of slicing Descriptions Examples
S[B:E] Produces new sequences
S[B:] Get list on left hand-side
S[:E] Get list on right hand-side
S[B:E]=[]
del S[B:E]
Delete a slice from
sequences
3.4 Sequence methods
 To find the positions of a value V, in a sequence S.
 S.index(V)
 Returns the number of elements S that are equal to V.
 S.count(V)
Izyan Yasmin 2017
3.5 List Method
 For any instances L, the.append(V)method appends a new value to the
list
 Insert new value, V at an arbitrary position, P using this expression
L.insert(P,V)
Izyan Yasmin 2017
 Method L.remove(V) removes the first element of L that equals V, if
there is one. If no elements equal to V, method raises a ValueError
exception.
Izyan Yasmin 2017
 L.sort(), method sorts the elements of a list into ascending order.
 L.reverse(), which reverses the elements in place.
Izyan Yasmin 2017
3.6 the range() function: creating arithmetic
progressions
 range(n), return a list containing an arithmetic progression.
 To generate a sequence [i, i+1, i+2, ..., n-1], use the form range(i, n)
 To generate an arithmetic progression with a difference d between successive
values, use the three-argument form range(i, n, d). The resulting sequence will be [i,
i+d, i+2*d, ...], and will stop before it reaches a value equal to n.
Izyan Yasmin 2017
3.7 One value can have multiple names
 It is necessary to be careful when modifying mutable values such as list
because there may be more than one name bound to the value.
 𝑉1, 𝑉2 = … . . = 𝑒𝑥𝑝𝑟
 We can make a new list using a slice that selects all the elements of menu1.
Izyan Yasmin 2017
Reference
 http://infohost.nmt.edu/tcc/help/pubs/lang/pytut27/pytut27.pdf
Izyan Yasmin 2017

More Related Content

What's hot

PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...Umesh Kumar
 
Sorting and searching arrays binary search algorithm
Sorting and searching arrays binary search algorithmSorting and searching arrays binary search algorithm
Sorting and searching arrays binary search algorithmDavid Burks-Courses
 
7 searching injava-binary
7 searching injava-binary7 searching injava-binary
7 searching injava-binaryirdginfo
 
2 data types and operators in r
2 data types and operators in r2 data types and operators in r
2 data types and operators in rDr Nisha Arora
 
[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++Muhammad Hammad Waseem
 
Data Structures- Part1 overview and review
Data Structures- Part1 overview and reviewData Structures- Part1 overview and review
Data Structures- Part1 overview and reviewAbdullah Al-hazmy
 
Data types in python
Data types in pythonData types in python
Data types in pythonRaginiJain21
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C ProgrammingDevoAjit Gupta
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0BG Java EE Course
 
Java presentation on insertion sort
Java presentation on insertion sortJava presentation on insertion sort
Java presentation on insertion sort_fahad_shaikh
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and ScalaFolding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and ScalaPhilip Schwarz
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsHoang Nguyen
 

What's hot (20)

PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
 
Sorting and searching arrays binary search algorithm
Sorting and searching arrays binary search algorithmSorting and searching arrays binary search algorithm
Sorting and searching arrays binary search algorithm
 
7 searching injava-binary
7 searching injava-binary7 searching injava-binary
7 searching injava-binary
 
Introduction to R for beginners
Introduction to R for beginnersIntroduction to R for beginners
Introduction to R for beginners
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
2 data types and operators in r
2 data types and operators in r2 data types and operators in r
2 data types and operators in r
 
[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++
 
C string
C stringC string
C string
 
1-D array
1-D array1-D array
1-D array
 
Data Structures- Part1 overview and review
Data Structures- Part1 overview and reviewData Structures- Part1 overview and review
Data Structures- Part1 overview and review
 
Data types in python
Data types in pythonData types in python
Data types in python
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
 
L10 sorting-searching
L10 sorting-searchingL10 sorting-searching
L10 sorting-searching
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
 
Java presentation on insertion sort
Java presentation on insertion sortJava presentation on insertion sort
Java presentation on insertion sort
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and ScalaFolding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Array ppt
Array pptArray ppt
Array ppt
 
Binary search
Binary searchBinary search
Binary search
 
Strings
StringsStrings
Strings
 

Similar to Basic python part 1

Module 3 - Regular Expressions, Dictionaries.pdf
Module 3 - Regular  Expressions,  Dictionaries.pdfModule 3 - Regular  Expressions,  Dictionaries.pdf
Module 3 - Regular Expressions, Dictionaries.pdfGaneshRaghu4
 
11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptxssuser8e50d8
 
regular-expression.pdf
regular-expression.pdfregular-expression.pdf
regular-expression.pdfDarellMuchoko
 
Chapter3 basic java data types and declarations
Chapter3 basic java data types and declarationsChapter3 basic java data types and declarations
Chapter3 basic java data types and declarationssshhzap
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptxDurgaNayak4
 
Numpy string functions
Numpy string functionsNumpy string functions
Numpy string functionsTONO KURIAKOSE
 
Arrays in python
Arrays in pythonArrays in python
Arrays in pythonmoazamali28
 
2 data structure in R
2 data structure in R2 data structure in R
2 data structure in Rnaroranisha
 
unit-4 regular expression.pptx
unit-4 regular expression.pptxunit-4 regular expression.pptx
unit-4 regular expression.pptxPadreBhoj
 
Array assignment
Array assignmentArray assignment
Array assignmentAhmad Kamal
 
The Datatypes Concept in Core Python.pptx
The Datatypes Concept in Core Python.pptxThe Datatypes Concept in Core Python.pptx
The Datatypes Concept in Core Python.pptxKavitha713564
 
16 Java Regex
16 Java Regex16 Java Regex
16 Java Regexwayn
 
stringsinpython-181122100212.pdf
stringsinpython-181122100212.pdfstringsinpython-181122100212.pdf
stringsinpython-181122100212.pdfpaijitk
 
Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular ExpressionsMukesh Tekwani
 
Python Datatypes by SujithKumar
Python Datatypes by SujithKumarPython Datatypes by SujithKumar
Python Datatypes by SujithKumarSujith Kumar
 

Similar to Basic python part 1 (20)

Python Strings.pptx
Python Strings.pptxPython Strings.pptx
Python Strings.pptx
 
Module 3 - Regular Expressions, Dictionaries.pdf
Module 3 - Regular  Expressions,  Dictionaries.pdfModule 3 - Regular  Expressions,  Dictionaries.pdf
Module 3 - Regular Expressions, Dictionaries.pdf
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptx
 
regular-expression.pdf
regular-expression.pdfregular-expression.pdf
regular-expression.pdf
 
Chapter3 basic java data types and declarations
Chapter3 basic java data types and declarationsChapter3 basic java data types and declarations
Chapter3 basic java data types and declarations
 
easyPy-Basic.pdf
easyPy-Basic.pdfeasyPy-Basic.pdf
easyPy-Basic.pdf
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptx
 
Numpy string functions
Numpy string functionsNumpy string functions
Numpy string functions
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
2 data structure in R
2 data structure in R2 data structure in R
2 data structure in R
 
unit-4 regular expression.pptx
unit-4 regular expression.pptxunit-4 regular expression.pptx
unit-4 regular expression.pptx
 
Array assignment
Array assignmentArray assignment
Array assignment
 
The Datatypes Concept in Core Python.pptx
The Datatypes Concept in Core Python.pptxThe Datatypes Concept in Core Python.pptx
The Datatypes Concept in Core Python.pptx
 
Python reference
Python referencePython reference
Python reference
 
16 Java Regex
16 Java Regex16 Java Regex
16 Java Regex
 
stringsinpython-181122100212.pdf
stringsinpython-181122100212.pdfstringsinpython-181122100212.pdf
stringsinpython-181122100212.pdf
 
Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular Expressions
 
Python Datatypes by SujithKumar
Python Datatypes by SujithKumarPython Datatypes by SujithKumar
Python Datatypes by SujithKumar
 

Recently uploaded

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 

Recently uploaded (20)

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 

Basic python part 1

  • 1. Python 2 Basic Python Part 1 Izyan Yasmin 2017
  • 2. Requirements  Python editor:  Spyder (Anaconda )  Python version: 2.7 and above Izyan Yasmin 2017
  • 3. 1.0 Python numeric types  Basic operation ( + , - , * , / ) To get the accurate answer, write in a decimal point. Because number without decimal point is consider as integer, a whole number. Two same function, but different approach. Print function will round-off the nearest number. Izyan Yasmin 2017
  • 4. 1.1 Assignment statement  Name can be attach to a value and the value will stay around for the rest of conversational Python session.  Python names must start with a letter or the underbar(_)character.  Examples:  How many seconds in 1 days? Izyan Yasmin 2017
  • 5.  General form of an assignments  𝑛𝑎𝑚𝑒1 = 𝑛𝑎𝑚𝑒2 = ⋯ = 𝑒𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛 It gives the same output, since it carries the same expression. Izyan Yasmin 2017
  • 6. 2.0 Character String Basics  In python you can choose to enclose strings either using single-quotes (‘…’) or double-quotes (“……”) Izyan Yasmin 2017 Print function will output the string without the quotes.
  • 7.  Converting string, 𝑠𝑡𝑟 to integer, 𝑖𝑛𝑡 or vice-versa Izyan Yasmin 2017
  • 8.  Python has several escape sequences. Izyan Yasmin 2017 If a line is not finished, python will displays a “….” prompt. Using print function, will gives the exact output in 3 lines. It will read directly the line and output using the special escape character n
  • 9. 2.1 indexing strings  To extract one or more characters from a string value, you need to know the position. The diagram shows the position between characters, which its starts before the first to the end of strings. Izyan Yasmin 2017
  • 10. 2.2 String methods Method General expression Examples center() To center text s.center(n) .ljust() and .rjust() Pad to length on left or right s.ljust(n) s.rjust(n) .strip(), .lstrip() and .rstrip() Remove leading and/or trailing whitespace s.strip() s.lstrip() s.rstrip() count() Search string s to see how many times t occur in string s s.count(t) Izyan Yasmin 2017 Many expression on strings are expressed as method. To call a method, we can use this syntax: • expr.method(𝑎𝑟𝑔1, 𝑎𝑟𝑔2, … ) If length isn’t enough, it will return the original values.
  • 11. Izyan Yasmin 2017 Method General expression Examples .find() and r.find() Locate string within a longer string s.find(t) .startswith() and .endswith() To check if a string s starts with a string t s.startswith(t) s.endswith(t) .lower() and .upper() Used to convert uppercased characters to lower and vice-versa s.lower() s.uppper() .split() Break field out of a strings s.split()
  • 12. 2.3 String format method  To combine string operations is to combine fixed text and variable values into single string.  General string of format operations:  s.format(𝑝0, 𝑝1, … , 𝑘0 = 𝑒 𝑜, 𝑘1 = 𝑒1)  Formatting of an item can be control using a format code of the form “{N:type}”, N is number of arguments to the . 𝑓𝑜𝑟𝑚𝑎𝑡() method, and type specifies details of the formatting. Izyan Yasmin 2017
  • 13. 3.0 Sequence types  A sequence in Python represents an ordered set.  str and unicode used to hold text, that is strings characters.  list and tuple used for sequence of zero or more values of any type.  Use list if the content of the sequence may change. Enclosed with square bracket “[…]”. [𝑒𝑥𝑝𝑟1, 𝑒𝑥𝑝𝑟2, … . ]  Use tuple if the contents may not change. Enclosed in parentheses “(….)” (𝑒𝑥𝑝𝑟1, 𝑒𝑥𝑝𝑟2, … . ) Izyan Yasmin 2017
  • 14. 3.1 Function and operators Description Operators Example Return numbers of elements in a sequence s len(s) Return largest value in a sequence s max(s) Return smallest value in sequence s min(s) To test set of membership in To concatenate two sequence of the same type + To get a new sequence containing n repetition of elements of s * Izyan Yasmin 2017
  • 15. 3.2 Indexing the position in a sequence  Positions in a sequence refer to locations between the values, using an expression s[i]. Izyan Yasmin 2017 Last line is an error because there is nothing in position 12 in string fruits.
  • 16. 3.3 Slicing sequences Izyan Yasmin 2017 Types of slicing Descriptions Examples S[B:E] Produces new sequences S[B:] Get list on left hand-side S[:E] Get list on right hand-side S[B:E]=[] del S[B:E] Delete a slice from sequences
  • 17. 3.4 Sequence methods  To find the positions of a value V, in a sequence S.  S.index(V)  Returns the number of elements S that are equal to V.  S.count(V) Izyan Yasmin 2017
  • 18. 3.5 List Method  For any instances L, the.append(V)method appends a new value to the list  Insert new value, V at an arbitrary position, P using this expression L.insert(P,V) Izyan Yasmin 2017
  • 19.  Method L.remove(V) removes the first element of L that equals V, if there is one. If no elements equal to V, method raises a ValueError exception. Izyan Yasmin 2017
  • 20.  L.sort(), method sorts the elements of a list into ascending order.  L.reverse(), which reverses the elements in place. Izyan Yasmin 2017
  • 21. 3.6 the range() function: creating arithmetic progressions  range(n), return a list containing an arithmetic progression.  To generate a sequence [i, i+1, i+2, ..., n-1], use the form range(i, n)  To generate an arithmetic progression with a difference d between successive values, use the three-argument form range(i, n, d). The resulting sequence will be [i, i+d, i+2*d, ...], and will stop before it reaches a value equal to n. Izyan Yasmin 2017
  • 22. 3.7 One value can have multiple names  It is necessary to be careful when modifying mutable values such as list because there may be more than one name bound to the value.  𝑉1, 𝑉2 = … . . = 𝑒𝑥𝑝𝑟  We can make a new list using a slice that selects all the elements of menu1. Izyan Yasmin 2017