This document outlines an agenda for a Python tutorial session, beginning with an introduction to Python's motivation and essentials like data types, operators, and flow control. It then provides examples of expressions, variables, and basic data structures like lists, tuples, and dictionaries. The session demonstrates Python blocks like conditionals, loops, functions, classes and modules. It concludes by suggesting areas for further practice like system administration, web development, and games.
12. TYPES IN PYTHON
#ASSIGNMENT
!
#CHECK FOO’S TYPE
!
#ANOTHER ASSIGNMENT
!
#FOO’S TYPE CHANGED
variable = 10
type(variable)
variable = “ten”
type(variable)
Dynamic
Implicit
x = 10 + “10” #ERROR
13. TYPES IN PYTHON Strings
line = “Hello”
multi = “”” I use this to write multiline
strings like html tags”””
raw = r’I’ll not replace n or t’ #used in regex
print(line[0]) #will print H try -3
template = "%d developers founded %s” % (3, ‘GDG Mansoura’)
#3 developers founded GDG Mansoura
‘{0}, {1}, {2}’.format(‘A’, ‘B’, ‘C’) #A, B, C
unicode_str = u’A unicode u018e string xf1'
14. TYPES IN PYTHON Boolean
1. It’s True not true
2. It’s False not false
15. TYPES IN PYTHON Data structures
List
names = [‘Ahmed’, ‘Sayed’, ‘AbdulHameed’]
first_name = names[0]
names[1] = “AlSayed” #names = [‘Ahmed’, ‘AlSayed’, ‘AbdulHameed’]
Tuple
family = (“AlSayed”, ”Reem”)
family[1] = “Some one else”
TypeError: 'tuple' object does not support item assignment
x = 5
y = 6
x, y = y, x
16. TYPES IN PYTHON Data structures
Dictionary
phonebook = {‘name’:”Ahmed Gamal”, ‘age’ : 28,
‘telephone’: ‘0123456789’,‘email’: ‘ahmed@example.com’}
Traversing Dictionary
for key in phonebook.keys(): print key
for val in phonebook.values(): print val
17. PYTHON BLOCKS
if condition:
If block
elif condition:
else If block
else:
else block
Branching
Where is a fixed indentation spaces or tabs
18. PYTHON BLOCKS
for item in xrange(10):
for block
else:
@ normal end
Loop
while true:
loop on this
else:
@ normal end
Normal end means no break
For is used basically for traversing iterable types (lists for example)
xrange is generating an iterable object
19. PYTHON BLOCKS Loop
found_obj = None
for obj in objects:
if obj.key == search_key:
found_obj = obj
break
else:
print (‘No object found.’)
20. PYTHON BLOCKS
class Car(Vehicle):
#constructor
def __init__(self, arg1):
pass
Class Definition
21. PYTHON BLOCKS
def foo(x,y):
function body
Functions
def foo(x,y=‘default’):
function body
def foo(x,y=‘default’):
function body
def foo(x,y):
return exprsn
def foo(x,y=‘default’):
function body
def foo(x,y=‘default’):
function body
23. BEFORE WE START
• If you are linux or unix based you are good to go.
• Else If you are on windows (I hope not) download
and install
• sublime text editor
• python runtime
• Our code will be perfect for Python 2.x and may be
3.x.
26. SCRIPT HIGHLIGHTS.
• Basic expression (variable assignment).
• User input and output.
• Function definition / passing function as variable.
• List data-type.
• Basic sorting.
27. MODULES AND PACKAGES
• from foo import bar
• from foor import * # please, don’t use it often
• Packages are directory with __init__.py file
• import this #Zen of python
28. NEXT: SHAPING YOUR EXPERIENCE
SYSTEM ADMIN, WEB, DESKTOP, NETWORK,
GAME DEVELOPMENT,…