introduction to Python by Mohamed Hegazy , in this slides you will find some code samples , these slides first presented in TensorFlow Dev Summit 2017 Extended by GDG Helwan
1. DEV SUMMIT 2017 EXTENDED
Introduction to Python 3
Mohamed Hegazy
R&D Engineer
.PY
2. DEV SUMMIT 2017 EXTENDED
What is python . WIKI
• Python is a widely used high-level programming language used for general-
purpose programming. An interpreted language, Python has a design philosophy
which emphasizes code readability (notably using whitespace indentation to
delimit code blocks rather than curly braces or keywords), and a syntax which
allows programmers to express concepts in fewer lines of code than possible in
languages such as C++ or Java.The language provides constructs intended to
enable writing clear programs on both a small and large scale.
• A scripting language is a subset of programming language that is used to produce
scripts, which are sets of instructions that automate tasks that would otherwise
be performed manually by a human. Of course, these "tasks" are essentially a
human giving instructions to a machine, as described above, so you could say that
a script instructs a machine on what instructions to give itself that a human
would otherwise give.
2
3. DEV SUMMIT 2017 EXTENDED
Agenda
1. Python Installation
2. Syntax
3. Objects
4. Conditions and Loops
5. Classes and Functions
6. Error Handling
7. Modules
8. Working with Files
9. Working With database
3
4. DEV SUMMIT 2017 EXTENDED
Python Installation
• Perquisites on windows.
• Download python for windows
https://www.python.org/downloads/windows/
• For this session we will use 3.6.0
• Download python for mac (for latest Update)
https://www.python.org/downloads/mac-osx/
• In Mac and Lunix its installed by default
• To run python use CMD -> Python
• Editors such as Pycharm , eclipse + pydev, Sublime, Aptana
4
5. DEV SUMMIT 2017 EXTENDED
Everything is an Object
• Id, type, value
• Id is unique identifier for the object
• Type is the class it is initialized from
• Value is the content
5
6. DEV SUMMIT 2017 EXTENDED
Syntax
• Python doesn’t use “int, double, string, var, etc…” as variable type.
Instead, python creates everything as an object and based on
assignment it calls the class and initialize
• Python doesn’t use “;”. It depends on whitespaces and indentation.
Wrong indentation means buggy code. P.S: you either use
whitespaces or tab.
• Example
• A = 0
• a,b,c = 0,1,”Hello”
6
7. DEV SUMMIT 2017 EXTENDED
Syntax Mutable vs immutable
• Number , Strings, Tuples are immutable.
• List , Dictionaries , other objects depending upon implementation.
7
8. DEV SUMMIT 2017 EXTENDED
Syntax
• When changing a value of an Immutable object. the reference of
value changes.
• Example
x=2
Id(x)
>>> 1231312
x=3
Id(x)
>>> 2349233
x=2
Id(x)
>>> 1231312
8
9. DEV SUMMIT 2017 EXTENDED
Syntax
• The variables created are stored in ram for later garbage collection to
pick it up. However there is a “faster” way using “del” keyword to
delete that variable
9
10. DEV SUMMIT 2017 EXTENDED
Syntax
• However, we can figure out the type of object with 2 ways.
1- the value
2- type(x) <- this will print out <class ‘str’>
a. it can be used to if want to make sure that the values are of certain type.
Note that print by default adds new line, it can be manipulated using
print (‘----’, end=‘’) <- end here will add zero space instead of newline
10
11. DEV SUMMIT 2017 EXTENDED
Syntax Dealing with Strings
• Strings can be used for chars and strings.
• Using single-quoted or double quoted is the same
• Since there is no char support, the work around is substring.
var1 = ‘hello world’
var2 = ‘python programming’
print(var1[0]) -> h
print(var2[8:]) -> programming
0 1 2 3 4
H E L L O
-5 -4 -3 -2 -1
11
12. DEV SUMMIT 2017 EXTENDED
Syntax Dealing with Strings
• r’string writenn here’ will result in string writenn here
• ‘String writenn here’ ->
• ‘My name is {} ‘. Format(“Mohamed hegazy”)
• My name is Mohamed hegazy
string write
n here
12
var = "hello World"
print(var[0]) # Single Char
print(var[5:]) # Range
print(var[:6]) # Range
print(var[:-1]) # Range
print("W" in var)
print('helntnn')
print(r'helntnn')
13. DEV SUMMIT 2017 EXTENDED
Syntax (*args and **kwargs)
• *Args prints data as tuple (remember tuple is immutable)
• **Kwargs prints and saves data as dictionary (remember dictionary is
mutable)
13
14. DEV SUMMIT 2017 EXTENDED
Conditions
• If condition doesn’t have “()”
Ex. If a<b:
print(‘a is bigger than b’)
• Python doesn’t support the switch statement, however there is a work
around.
If v == ‘----’:
Print(“this is first one”)
elif v == ‘qwdqwdqq’:
print(“this is the second one”)
else:
print(“this is not in the switch case”)
and, or, not
14
15. DEV SUMMIT 2017 EXTENDED
Loops
• While loop.
• For loop
For i = 0, i<x , i++ -> it doesn’t exist
15
while i < 10:
print(i)
i += i
for line in cursor.fetchall():
print(line, end=" ")
16. DEV SUMMIT 2017 EXTENDED
Defining Classes and functions
• Class is how to create your own object
• Class is a blueprint for an object
• Object is instance of a class
• Classes can be used in inheritance
16
18. DEV SUMMIT 2017 EXTENDED
Defining Classes
18
if __name__ == '__main__':main()
19. DEV SUMMIT 2017 EXTENDED
Error Handling
• Python error handling, Mitigating the app from crashing on error.
• Raising Exception
• Raise is used to create custom exceptions
• Ex. Have a .docx file and .txt file. Mainly, my data is stored in .txt file,
therefore I created a custom exception to notify the user to only use. Txt file
19
21. DEV SUMMIT 2017 EXTENDED
Power of Yield
• Yield is like the return . However, yield can be used to return the
argument and continue the loop
• def __iter__(self):
i = self.start
while(i <= self.stop):
yield i
i += self.step
21
22. DEV SUMMIT 2017 EXTENDED
modules
• Modules are used to add “extension/inheritance” .
• Basic Python support limited functions comparing to modules.
• Using PIP and Easy_install
• Ex. Adding web functionality to python .
• Import Django
• Import sqlite3
Ex.
Import Django (as d
Django.Version
pinrt(Django.get_version())
22
23. DEV SUMMIT 2017 EXTENDED
File I/O
• Read from file
• Write to file
• Read from file binary
• Write to file binary
• Buffer read , Buffer write
• appending
23