Download - Introduction To Programming with Python-3
![Page 1: Introduction To Programming with Python-3](https://reader033.vdocument.in/reader033/viewer/2022061616/5563a4b0d8b42a01658b5397/html5/thumbnails/1.jpg)
Introduction to Programming with Python – Class 3SYED FARJAD ZIA ZAIDI
![Page 2: Introduction To Programming with Python-3](https://reader033.vdocument.in/reader033/viewer/2022061616/5563a4b0d8b42a01658b5397/html5/thumbnails/2.jpg)
Class Objectives
Class Objective
Understanding Strings and some useful string operations.
Write few simple programs that use string functions and lists.
![Page 3: Introduction To Programming with Python-3](https://reader033.vdocument.in/reader033/viewer/2022061616/5563a4b0d8b42a01658b5397/html5/thumbnails/3.jpg)
Class Material
• Chapter 6, 8 - Python for Informatics: Exploring Information
Reading
![Page 4: Introduction To Programming with Python-3](https://reader033.vdocument.in/reader033/viewer/2022061616/5563a4b0d8b42a01658b5397/html5/thumbnails/4.jpg)
Strings
Strings are amongst the most popular types in Python. We can create them simply by enclosing characters in quotes. Python treats single quotes the same as double quotes.
Creating strings is as simple as assigning a value to a variable. For example:
message = “Hello World”
![Page 5: Introduction To Programming with Python-3](https://reader033.vdocument.in/reader033/viewer/2022061616/5563a4b0d8b42a01658b5397/html5/thumbnails/5.jpg)
Introduction to Indexing Strings can be divided into substrings using indexing.
To access substrings, use the square brackets for slicing along with the index or indices to obtain your substring, this is called Indexing. Following is a simple example:
message = 'Hello World!'
messageTwo = "Python Programming"
print "var1[0]: ", message[0]
print "var2[1:5]: ", messageTwo[1:5]
![Page 6: Introduction To Programming with Python-3](https://reader033.vdocument.in/reader033/viewer/2022061616/5563a4b0d8b42a01658b5397/html5/thumbnails/6.jpg)
String Concatenation
String concatenation is a process in which two strings are merged together. Concatenation is very simple and uses the operator ‘+’ to concatenate.
For Example:
message = “Hello”
messageTwo = “ “
messageThree = “World”
print message + messageTwo + messageThree
![Page 7: Introduction To Programming with Python-3](https://reader033.vdocument.in/reader033/viewer/2022061616/5563a4b0d8b42a01658b5397/html5/thumbnails/7.jpg)
Escape Characters
Following table is a list of commonly used escape or non-printable characters that can be represented with backslash notation.
An escape character gets interpreted; in a single quoted as well as double quoted strings.Backslash Notation Description
\n New Line
\t Space
\s Tab
![Page 8: Introduction To Programming with Python-3](https://reader033.vdocument.in/reader033/viewer/2022061616/5563a4b0d8b42a01658b5397/html5/thumbnails/8.jpg)
String Special Operators
+
• Concatenation - Adds values on either side of the operator
*• Repetition -
Creates new strings, concatenating multiple copies of the same string
![Page 9: Introduction To Programming with Python-3](https://reader033.vdocument.in/reader033/viewer/2022061616/5563a4b0d8b42a01658b5397/html5/thumbnails/9.jpg)
Python Comparison Operators
• Slice - Gives the character from the given index
[]
• Range Slice - Gives the characters from the given range
[ : ]
![Page 10: Introduction To Programming with Python-3](https://reader033.vdocument.in/reader033/viewer/2022061616/5563a4b0d8b42a01658b5397/html5/thumbnails/10.jpg)
Python Comparison Operators
• Membership - Returns true if a character exists in the given string
in
• Membership - Returns true if a character does not exist in the given string
not in
![Page 11: Introduction To Programming with Python-3](https://reader033.vdocument.in/reader033/viewer/2022061616/5563a4b0d8b42a01658b5397/html5/thumbnails/11.jpg)
Python Triple Quotes
Python's triple quotes comes to the rescue by allowing strings to span multiple lines, including verbatim NEWLINEs, TABs, and any other special characters.
![Page 12: Introduction To Programming with Python-3](https://reader033.vdocument.in/reader033/viewer/2022061616/5563a4b0d8b42a01658b5397/html5/thumbnails/12.jpg)
String Functions:Method Description
capitalize() Capitalizes first letter of string
count(str, beg=0, end=len(string)) Counts how many times str occurs in string or in a substring of string if starting index beg and ending index end are given
find(str, beg=0, end=len(string)) Determine if str occurs in string or in a substring of string if starting index beg and ending index end are given returns index if found and -1 otherwise
index(str, beg=0, end=len(string)) Same as find(), but raises an error if str not found
len(string) Returns the length of the string
![Page 13: Introduction To Programming with Python-3](https://reader033.vdocument.in/reader033/viewer/2022061616/5563a4b0d8b42a01658b5397/html5/thumbnails/13.jpg)
String Functions:
Go to the following link for more string functions:
http://www.tutorialspoint.com/python/python_strings.htm
You can also find string functions by writing the following in the python IDLE
help(str)
![Page 14: Introduction To Programming with Python-3](https://reader033.vdocument.in/reader033/viewer/2022061616/5563a4b0d8b42a01658b5397/html5/thumbnails/14.jpg)
Exercise 1
Write code using find() and string slicing to extract the number at the end of the line below. Convert the extracted value to a floating point number and print it out.
text = "X-DSPAM-Confidence: 0.8475";
![Page 15: Introduction To Programming with Python-3](https://reader033.vdocument.in/reader033/viewer/2022061616/5563a4b0d8b42a01658b5397/html5/thumbnails/15.jpg)
Exercise 2 Given a string and a non-negative int n, return a larger string that is n
copies of the original string.
string_times('Hi', 2) → 'HiHi'string_times('Hi', 3) → 'HiHiHi'string_times('Hi', 1) → 'Hi'
![Page 16: Introduction To Programming with Python-3](https://reader033.vdocument.in/reader033/viewer/2022061616/5563a4b0d8b42a01658b5397/html5/thumbnails/16.jpg)
Exercise 3 Given a string, return a string where for every char in the original,
there are two chars.
double_char('The') → 'TThhee'
double_char('AAbb') → 'AAAAbbbb'
double_char('Hi-There') → 'HHii--TThheerree'
![Page 17: Introduction To Programming with Python-3](https://reader033.vdocument.in/reader033/viewer/2022061616/5563a4b0d8b42a01658b5397/html5/thumbnails/17.jpg)
Any Queries?
Link to Facebook Group:
https://www.facebook.com/groups/introtopython.iccbs/
Email: