Python Presentation 1
Python Presentation 1
Contents
• Introduction to Python
• Installing python
• Numbers, variables & basic operations
• Data Types – Lists, tuples & dictionary
• Various operators
• Defining functions
• Defining Classes
• Inheritance
What is Python ?
Python is Object-Oriented
• Structure supports concepts as Polymorphism,
Operation overloading & Multiple Inheritance
Python 2.x is legacy, Python 3.x is the present and future of the language.
The most visible difference is probably the way the “print” statement
works.
In python2 it’s a statement
print “Hello World!”
If you are running new version of Python, then you would need to use print
statement with parenthesis as in
Hello, Python!
Python Strings:
Lists are the most versatile of Python's compound data types. A list contains
items separated by commas and enclosed within square brackets ([]).
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
For loop
while expression:
statements(s)
Defining Functions
Argument Return
Function
Defining Functions
Example:
def printme( str ):
"This prints a passed string into this function"
print(str)
return
Class
>>> p=B()
>>> p.hello()
I am Super Class
>>> p.bye()
I am Sub Class
Using in-built Modules
Import serial
Ser=serial.Serial(‘COM4’,9600)
Floor Division - The division of operands where the 9//2 = 4 and 9.0//2.0 = 4.0, -11//3 = -4, -
result is the quotient in which the digits after the 11.0//3 = -4.0
decimal point are removed. But if one of the operands
// is negative, the result is floored, i.e., rounded away
from zero (towards negative infinity):
Python Comparison Operators
Operator Description Example
If the values of two operands are equal, then the condition
== (a == b) is not true.
becomes true.
If values of two operands are not equal, then condition
!=
becomes true.
If the value of left operand is greater than the value of right
> (a > b) is not true.
operand, then condition becomes true.
If the value of left operand is less than the value of right
< (a < b) is true.
operand, then condition becomes true.
If the value of left operand is greater than or equal to the
>= (a >= b) is not true.
value of right operand, then condition becomes true.
If the value of left operand is less than or equal to the value
<= (a <= b) is true.
of right operand, then condition becomes true.
Python Assignment Operators
Operator Description Example
Assigns values from right side operands to left side c = a + b assigns value of a + b into
=
operand c
It adds right operand to the left operand and assign the
+= Add AND c += a is equivalent to c = c + a
result to left operand
It divides left operand with the right operand and assign c /= a is equivalent to c = c / ac /= a
/= Divide AND
the result to left operand is equivalent to c = c / a
%= Modulus It takes modulus using two operands and assign the result
c %= a is equivalent to c = c % a
AND to left operand
Python Logical Operators
Operator Description Example
not Logical NOT Used to reverse the logical state of its operand. Not(a and b) is false.
Python Membership Operators
Operator Description Example
Evaluates to true if it finds a variable in
x in y, here in results in a 1 if x is a member
in the specified sequence and false
of sequence y.
otherwise.
Evaluates to true if it does not find a
x not in y, here not in results in a 1 if x is
not in variable in the specified sequence and
not a member of sequence y.
false otherwise.
NUMBERS
Python Number Conversion
– Type int(x) to convert x to a plain integer.
– Type long(x) to convert x to a long integer.
– Type float(x) to convert x to a floating-point number.
– Type complex(x) to convert x to a complex number
with real part x and imaginary part zero.
– Type complex(x, y) to convert x and y to a complex
number with real part x and imaginary part y. x and y
are numeric expressions
NUMBERS
String Parsing
• Print("My name is %s and weight is %d kg!" % ('Zara', 21) )
Format Symbol Conversion
%c character
%s string conversion via str() prior to formatting
%i signed decimal integer
%d signed decimal integer
%u unsigned decimal integer
%o octal integer
%x hexadecimal integer (lowercase letters)
%X hexadecimal integer (UPPERcase letters)
%e exponential notation (with lowercase 'e')
%E exponential notation (with UPPERcase 'E')
Sets
• A set contains an unordered collection of unique and immutable objects.
The set data type is, as the name implies, a Python implementation of the
sets as they are known from mathematics. This explains, why sets unlike
lists or tuples can't have multiple occurrences of the same element.
>>> adjectives = {"cheap","expensive","inexpensive","economical"}
>>> adjectives
set(['inexpensive', 'cheap', 'expensive', 'economical']) >>>
• Exception Handling
• Assertions:
What is Exception?
• An exception is an event, which occurs during the execution of a
program that disrupts the normal flow of the program's instructions. In
general, when a Python script encounters a situation that it cannot
cope with, it raises an exception. An exception is a Python object that
represents an error.
• In the game of Hangman, a clue word is given by the program that the player has to
guess, letter by letter. The player guesses one letter at a time until the entire word has
been guessed. (In the actual game, the player can only guess 6 letters incorrectly
before losing).
• Let’s say the word the player has to guess is “EVAPORATE”. For this exercise, write the
logic that asks a player to guess a letter and displays letters in the clue word that were
guessed correctly. For now, let the player guess an infinite number of times until they
get the entire word. As a bonus, keep track of the letters the player guessed and
display a different message if the player tries to guess that letter again. Remember to
stop the game when all the letters have been guessed correctly!
• https://www.google.com/settings/security/lesssecureapps