Py Slides 1
Py Slides 1
Python Overview
• Python is a high-level, interpreted, interactive and object
oriented-scripting language.
• Single-Line Comments:
• Single-line comments begin with the # Symbol
• # This is a single-line comment
• x = 10 # This is an inline comment
• Multi-Line Comments
• ‘’’ or “”” are often used for block comments
• """
• This is a multi-line comment
• using triple quotes.
• It spans multiple lines.
• """
Using Blank Lines:
• def farewell():
• print("Goodbye, World!")
Operat
Description Example
or
& Binary AND Operator copies a bit to the (a & b) will give0 which
result if it exists in both operands. is 0000 0000
| Binary OR Operator copies a bit if it exists (a | b) will give 30 which
in either operand. is 0001 1110
^ Binary XOR Operator copies the bit if it is (a ^ b) will give 30
set in one operand but not both. which is 0001 1110
~ Binary Ones Complement Operator is unary (~a ) will give -10 which
and has the effect of 'flipping' bits. is 0101
<< Binary Left Shift Operator. The left a << 2 will give 40
operands value is moved left by the which is 0010 1000
number of bits specified by the right
operand.
>> Binary Right Shift Operator. The left a >> 2 will give 2 which
operands value is moved right by the is 0000 0010
number of bits specified by the right
operand.
Python Logical Operators:
Operat
Description Example
or
and Called Logical AND operator. If both the (a and b) is true. X=5,
operands are true then then condition a>3 and a<10
becomes true.
or Called Logical OR Operator. If any of the two (a or b) is true.
operands are non zero then then condition
becomes true.
not Called Logical NOT Operator. Use to not(a and b) is false.
reverses the logical state of its operand. If a
condition is true then Logical NOT operator
will make false.
Python Membership
Operators:
In addition to the operators discussed previously, Python has
membership operators, which test for membership in a
sequence, such as strings, lists, or tuples.
Operato
Description Example
r
in Evaluates to true if it finds a variable in the x in y, here in results in a
specified sequence and false otherwise. 1 if x is a member of
sequence y.
Output:
Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST
Python If ... Else
• a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
Else
•a=2
b = 330
print("A") if a > b else print("B")
• a = 330
b = 330
print("A") if a > b else print("=") if a ==
b else print("B")
And
• a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")
Or
• a = 200
b = 33
c = 500
if a > b or a > c:
print("At least one of the conditions is
True")
Not
• a = 33
b = 200
if not a > b:
print("a is NOT greater than b")
Nested If
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
Python While Loops
•i=1
while i < 6:
print(i)
if i == 3:
break
i += 1
The continue Statement
•i=0
while i < 6:
i += 1
if i == 3:
continue
print(i)
The else Statement
•i=1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
Python Lists:
• Lists are the most versatile of Python's compound data
types. A list contains items separated by commas and
enclosed within square brackets ([]).
• To some extent, lists are similar to arrays in C. One
difference between them is that all the items belonging to
a list can be of different data type.
• The values stored in a list can be accessed using the slice
operator ( [ ] and [ : ] ) with indexes starting at 0 in the
beginning of the list and working their way to end-1.
• The plus ( + ) sign is the list concatenation operator, and
the asterisk ( * ) is the repetition operator.
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john']
Output:
['abcd', 786, 2.23, 'john', 70.2]
abcd
[786, 2.23]
[2.23, 'john', 70.2]
[123, 'john', 123, 'john']
['abcd', 786, 2.23, 'john', 70.2, 123, 'john']
Python Tuples:
• A tuple is another sequence data type that is similar to the
list. A tuple consists of a number of values separated by
commas. Unlike lists, however, tuples are enclosed within
parentheses.
• The main differences between lists and tuples are: Lists are
enclosed in brackets ( [ ] ), and their elements and size can
be changed, while tuples are enclosed in parentheses ( ( ) )
and cannot be updated. Tuples can be thought of as read-
only lists.
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
tinytuple = (123, 'john')
OUTPUT:
('abcd', 786, 2.23, 'john', 70.2)
abcd
(786, 2.23)
(2.23, 'john', 70.2)
(123, 'john', 123, 'john')
('abcd', 786, 2.23, 'john', 70.2, 123, 'john')
Python Sets:
• Python 's dictionaries are hash table type. They work like
associative arrays or hashes found in Perl and consist of
key-value pairs.
• Keys can be almost any Python type, but are usually
numbers or strings. Values, on the other hand, can be any
arbitrary Python object.
• Dictionaries are enclosed by curly braces ( { } ) and values
can be assigned and accessed using square braces ( [] ).
dict = {}
dict['one'] = "This is one"
dict[2] = "This is two“
tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
print dict['one'] # Prints value for 'one' key
print dict[2] # Prints value for 2 key
print tinydict # Prints complete dictionary
print tinydict.keys() # Prints all the keys
print tinydict.values() # Prints all the values
OUTPUT:
This is one
This is two
{'dept': 'sales', 'code': 6734, 'name': 'john'}
['dept', 'code', 'name']
['sales', 6734, 'john']
Data Type Conversion:
Function Description
int(x [,base]) Converts x to an integer. base specifies the base if x is a string.
long(x [,base] ) Converts x to a long integer. base specifies the base if x is a string.
float(x) Converts x to a floating-point number.
complex(real Creates a complex number.
[,imag])
str(x) Converts object x to a string representation.
repr(x) Converts object x to an expression string.
eval(str) Evaluates a string and returns an object.
tuple(s) Converts s to a tuple.
list(s) Converts s to a list.
set(s) Converts s to a set.
dict(d) Creates a dictionary. d must be a sequence of (key,value) tuples.
frozenset(s) Converts s to a frozen set.
chr(x) Converts an integer to a character.
unichr(x) Converts an integer to a Unicode character.
ord(x) Converts a single character to its integer value.
hex(x) Converts an integer to a hexadecimal string.
oct(x) Converts an integer to an octal string.