Professional Documents
Culture Documents
Python PPT 03
Python PPT 03
Python PPT 03
n
Introduction
What can python
do?
• Instagram
• Spotify
• Netflix
• Google
• Dropbox
Why
Python?
• Readable and Maintainable Code
• Multiple Programming Paradigms
• Compatible with Major Platforms and Systems
• Robust Standard Library
• Many Open Source Frameworks and Tools
• Web app development
• Data science
• Scripting
• Database programming
• Quick prototyping
Comparison with
Java
• Java • Python
Example:
name=input("Enter your name: ")
Comment
s
In a Python script, the symbol # indicates the start of a comment line. It iseffective
till the end of the line in the editor. If # is the first character of the line, then the
entire line is a comment. It can be used also in the middle of a line. The textbefore
it is a valid Python expression, while the text following is treated as acomment.
Example:
# this is a comment
print ("Hello World")
print ("Welcome to
Python Tutorial")
#this is also a
comment but after a
statement.
Variables
Any value of certain type is stored
in the computer's memory for
processing. Out of available
100
memory locations, one is
randomly allocated for storage. In
order to conveniently and 104
• In addition you can specify if the file should be handled as binary or text
mode
"t" - Text - Default value. Text mode
"b" - Binary - Binary mode (e.g. images)
• Because "r" for read, and "t" for text are the
default values, you do not need to specify
Read.
.The open() function returns a file object, which has a read() method for reading the
content of the file. By default the read() method returns the whole text, but you
can also specify how many characters you want to return:
f = open("demofile.txt", "r")
print(f.read())# print(f.read(5))
Can return one line by using the readline() method:
f = open("demofile.txt", "r")
print(f.readline())
def iter
(self): return
self
def next
(self): if self.i <
self.n:
i = self.i
self.i += 1
return i
else:
raise
StopIte
Generator
s
• Generators simplifies creation of iterators. A generator is a function
that produces a sequence of results instead of a single value.
def yrange(n):
i=0
while i < n:
yield i
i += 1
Condition Return Value
• The any() function returns One value is true (others are false) True
True if any element of an
iterable is True. If not,
any() returns False.
any(iterable) One value is false (others are true) True
Empty Iterable
True
With
statement
• with statement in Python is used in exception handling to make the
code cleaner and much more readable. It simplifies the management
of common resourc# using with statement
with open('file_path', 'w') as file:
file.write('hello world !')
Context
Management
• A high-level explanation of the context management protocol is:
• The expression is evaluated and should result in an object called a “context manager”. The context manager
must have enter () and exit () methods.
• The context manager’s enter () method is called. The value returned is assigned to
VAR. If no as VAR clause is present, the value is simply discarded.
• The code in BLOCK is executed.
• If BLOCK raises an exception, the context manager’s exit () method is called with three arguments,
the exception details (type, value, traceback, the same values returned by sys.exc_info(), which can also
be None if no exception occurred). The method’s return value controls whether an exception is re-raised:
any false value re-raises the exception, and True will result in suppressing it. You’ll only rarely want to
suppress the exception, because if you do the author of the code containing the ‘with’ statement will never
realize anything went wrong.
• If BLOCK didn’t raise an exception, the exit () method is still called, but type, value, and traceback
are all None.
Working
# a simple file writer object
class MessageWriter(object):
def init (self, file_name):
self.file_name = file_name
MetaCharacters
Metacharacters are characters that are interpreted in a special way by a RegEx engine. Here's a list of metacharacters:
[] . ^ $ * + ? {} () \ |
[] - Square brackets
Square brackets specifies a set of characters you wish to match.
Contd.
.Period
• A period matches any single character (except newline '\n’).
^ - Caret
• The caret symbol ^ is used to check if a string starts with a certain character
Dollar
• The dollar symbol $ is used to check if a string ends with a certain character.
Star
• The star symbol * matches zero or more occurrences of the pattern left to it.
Question Mark
• The question mark symbol ? matches zero or one occurrence of the pattern left to it.
Contd.
.
{} - Braces
• Consider this code: {n,m}. This means at least n, and at most m repetitions of the pattern left to it.
() - Group
• Parentheses () is used to group sub-patterns. For example, (a|b|c)xz match any string thatmatches
either a or b or c followed by xz
\ - Backslash
• Backlash \ is used to escape various characters including all metacharacters. For example,
• \$a match if a string contains $ followed by a. Here, $ is not interpreted by a RegEx engine in a
special way.
• If you are unsure if a character has special meaning or not, you can put \ in front of it. This makes sure the
character is not treated in a special way.
Contd.
.• \A - Matches if the specified characters are at the start of a string.
• \b - Matches if the specified characters are at the beginning or end of a word.
• \B - Opposite of \b. Matches if the specified characters are not at the beginning or end of a word.
• \d - Matches any decimal digit. Equivalent to [0-9]
• \D - Matches any non-decimal digit. Equivalent to [^0-9]
• \s - Matches where a string contains any whitespace character.
• \S - Matches where a string contains any non-whitespace character.
• \w - Matches any alphanumeric character (digits and alphabets).
• \W - Matches any non-alphanumeric character
• \Z - Matches if the specified characters are at the end of a string.
Client Server
Python