Python training slides for beginners that I lecture. Starts from python versions to popular 3 party libraries. Arguments, interactive modes, variable types, language keywords, functions, class etc. If you want to get training please look www.ismailbaydan.com for more details.
2. Interpreting
Read script file and run the instructions
/usr/local/bin/python3.4
C:Python34
set path=%path%;C:python34
Control-D on Unix
Control-Z on Windows
python -c command [arg] ...
Python -h
4. Interactive Mode
$ python3.4
Python 3.4 (default, Mar 16 2014, 09:25:04)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for
more information.
>>>
9. Strings
Single or double quote
>>> 'spam eggs' # single quotes
'spam eggs'
>>> 'doesn't' # use ' to escape the single
quote...
"doesn't"
>>> "doesn't" # ...or use double quotes instead
"doesn't"
10. Strings
escape character
>>> '"Yes," he said.'
'"Yes," he said.'
>>> ""Yes," he said."
'"Yes," he said.'
>>> '"Isn't," she said.'
'"Isn't," she said.'
Raw strings
>>> print('C:somename') # here n means
13. Strings
Automatic concatenate
>>> 'Py' 'thon'
'Python'
>>> prefix + 'thon'
'Python'
Long strings
>>> text = ('Put several strings within
parentheses '
'to have them joined together.')
14. Strings
Index
>>> word = 'Python'
>>> word[0] # character in position 0
'P'
>>> word[5] # character in position 5
'n'
>>> word[-1] # last character
'n'
>>> word[-2] # second-last character
15. Strings
>>> word[0:2] # characters from position 0
(included) to 2 (excluded)
'Py'
>>> word[2:5] # characters from position 2
(included) to 5 (excluded)
'tho'
>>> word[42] # the word only has 6 characters
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
26. If
Selection specific condition
>>> if x < 0:
... x = 0
... print('Negative changed to zero')
... elif x == 0:
... print('Zero')
... elif x == 1:
... print('Single')
... else:
41. Documentation
First line short and no name
First line big
>>> def my_function():
... """Do nothing, but document it.
...
... No, really, it doesn't do anything.
... """
... pass
43. Coding Style
Use 4-space indentation, and no tabs.
Wrap lines so that they don’t exceed 79
characters
Use blank lines to separate functions and
classes, and larger blocks of code inside
functions.
When possible, put comments on a line of their
own.
Use docstrings.
Use spaces around operators and after
44. List as Stacks
Using append and pop
>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7
45. Lists as Queues
First in – First out
>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue.append("Terry")
>>> queue.popleft()
52. Dictionarries-Looping
>>> for k, v in knights.items():
... print(k, v)
Enumerate with index
>>> for i, v in enumerate(['tic', 'tac', 'toe']):
... print(i, v)
53. Looping -Reversed,Sorted
Reverse it please
>>> for i in reversed(range(1, 10, 2)):
... print(i)
Sorte
>>> basket = [ 'apple', 'pear', 'orange', 'banana']
>>> for f in sorted(set(basket)):
... print(f)
54. Conditions – in is
Use in value occurence
Use is object
Use not for other
64. Input & Output
Human-readable form
Write values
Print function
Expression statements
Write function
Str.format()
65. Input & Output
Packages
>>> for x in range(1, 11):
... print('{0:2d} {1:3d} {2:4d}'.format(x, x*x,
x*x*x))
Fill left with zeros
>>> '12'.zfill(5)
'00012'
66. Input & Output
>>> print('We are the {} who say
"{}!"'.format('knights', 'Ni'))
We are the knights who say "Ni!"
67. Input & Output - File
Reading&writing file
>>> f = open('workfile', 'w')
W,r,R+,a
Modes
Text
Binary b
68. Input & Output – File
Readiing
>>> f.read()
'This is the entire file.n'
Read just line
>>> f.readline()
Read lines with loop
>>> for line in f:
... print(line, end='')
71. Errors & Exceptions -Syntax Error
>>> while True print('Hello world')
File "<stdin>", line 1, in ?
while True print('Hello world')
^
SyntaxError: invalid syntax
72. Errors & Exceptions - Exceptions
>>> 10 * (1/0)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ZeroDivisionError: division by zero
73. Errors & Exceptions – Handling
Exceptions
Try and except code block
try:
f = open('myfile.txt')
s = f.readline()
i = int(s.strip())
except OSError as err:
print("OS error: {0}".format(err))
except ValueError:
print("Could not convert data to an integer.")