A brief introduction by example of the one of the most popular programming languages. These simple introductory slides are intended to provide a basic introduction to Python to those individuals who already have some elementary programming experience.
2. @DylanSeychell
What is Python?
● Widely used and supported
● Interpreted Scripting Language
● Focuses on Readability and Productivity
● Multipurpose:
○ Web Programming
○ GUI Applications
○ Computer Vision Apps
○ Data Science
○ And much more…
○ But no, not yet adequate for mobile programming :(
2
3. @DylanSeychell
Installing Python
● Python can be downloaded from the official site:
https://www.python.org/downloads/
○ Further libraries would need to be configured manually.
● You may also download Python from the Anaconda platform through this link:
https://www.anaconda.com/download/
○ This approach facilitates the management of different environments and its benefits would be
more tangible at a later stage.
3
5. @DylanSeychell
Using Python from the interactive shell
From your Terminal/CMD, type ‘python’ to get started
Dylans-MacBook-Pro-3:~ dylanseychell$ python
A successful configuration will return something like the following:
Python 2.7.13 |Anaconda custom (x86_64)| (default, Dec 20 2016, 23:05:08)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>>
You may now use Python shell by typing commands after the >>>
To quit Python in terminal, simply type exit()
5
7. @DylanSeychell
Comparisons
Python can processes logical/comparison operations and return a Boolean result
accordingly:
>>> 3>4
False
>>> 2==2
True
>>> 3!=1
True
>>> 3 < (15/3)
True
>>> 4 > (3*3)
False
7
8. @DylanSeychell
Declaring Variables
Variables are declared by simply naming them and assigning an initial value. The
variable type is not declared.
Declaring a variable does not return specific feedback.
>>> a = 3
>>>
You may however check the content of the variable by calling it after declaration.
>>> a
3
8
9. @DylanSeychell
Using Variables to store numbers
Variables and mathematical operations
>>> a = 3
>>> a + 1
4
Values would be updated accordingly and they can be reused.
>>> a * 2
8
9
10. @DylanSeychell
Variables and Strings
Text is stored in the same way
>>> s = "hello"
>>> s
'hello'
It’s also easy to concatenate text by using the + operator
>>> s + " world"
'hello world'
Or maybe, replicate for defined number of times by using the * operator
>>> s*3
'hellohellohello'
10
12. @DylanSeychell
Functions on Strings
You may need to check the length of a string (or object). Just use len()
>>> len (s) #s still contains ‘hello’
5
Or convert to upper or lower case
>>> s.upper()
'HELLO'
>>> s.lower()
'Hello'
12
13. @DylanSeychell
Accessing characters in Strings
Index of characters starts from zero (left) incrementing towards the right
Characters may also be accessed by their distance from the end, this time -1 means
the first from the end (can’t have -0).
13
14. @DylanSeychell
Accessing characters in a String (examples)
Characters can be accessed by the use of [] like most other popular languages.
>>> s[0]
'h'
>>> s[3]
'l'
They can also be accessed with reference to their position from the end of the
string.
>>> s[-5]
'h'
>>> s[-1]
'o'
14
15. @DylanSeychell
Slicing Strings
Strings can be sliced by using the s[start:end] notation. This allows us to
extract a substring that falls within the specified range. End is not included.
>>> s[1:3]
'el'
>>> s[3:4]
'l'
>>> s[1:4]
'ell'
If the end index is greater than the length, the result is truncated.
>>> s[1:100]
'ello'
15
17. @DylanSeychell
Using Python from an editor
The approach to write more complex Python code
requires you to write code in a file.
Python module files must have a .py suffix
These can be written through any text editing
software.
I personally prefer Atom (http://atom.io) particularly
because you can also have a terminal screen as
part of the editor and install a nice variety of plugins.
To execute a script, find the designated folder from
the terminal and type python scriptName.py
17
19. @DylanSeychell
Functions
Functions in Python are defined by writing the keyword def followed by the name
of the function and parameters. This has to be followed by a colon : and the
code-block has to be tabbed.
def show(a):
print(a)
This is then simply called anywhere else in the module.
show(“hello”) #prints “hello” to console
19
21. @DylanSeychell
IF Statements
Based on the same concept traditional programming languages where a
code-block is executed when the testing condition is true.
a = 3
if a > 2:
print ("a is > than 2")
Notes:
1) That the testing condition did not need to be surrounded by round brackets
2) The code block is established by a colon : after the testing condition and the
statements were then indented by a tab.
21
22. @DylanSeychell
ELIF (else-if) and ELSE
The elif statement is used to to cover other logical testing conditions.
a = 3
if a > 2:
print ("a is > than 2")
elif a < 2:
print ("a is < than 2")
The else statement is used to cover all other possibilities.
else:
print ("a is equal to 2")
22
24. @DylanSeychell
Logical Operators
Python does not use the C-style logical operators (&&, || and !)
The keywords and, or and not are used instead.
if a > 2 and a < 4:
print ("a must be 3")
24
25. @DylanSeychell
Lists
One of Python’s built-in features and can be initialised by []
months = []
List literals are stored in square brackets []
months = ['March', 'May', 'September']
print (months)
Items may be accessed by their index.
print (months[1]) ##returns May
25
26. @DylanSeychell
Adding items to lists
The append method allows us to add new items to a list.
months.append("December")
The insert method allows us to add new items to a list at a specific index
months.insert(0, "January")
26
27. @DylanSeychell
Methods on lists
The length of the list may be checked by using len
print (len(months)) #returns 3
Lists may be reversed by the calling the reverse function (does not return)
months.reverse()
print (months) ## returns ['September', 'May', 'March']
27
28. @DylanSeychell
FOR construct
The for construct is used in Python to iterate through lists or other structures. This
uses a var in list pattern, where the var would be an iterator of the same type of
the list.
months = ['March', 'May', 'September']
for month in months:
print (month)
The above prints the contents of this list.
28
29. @DylanSeychell
IN Construct
The in construct can also be used to to test whether an element exists in a list.
if 'May' in months:
print ("I know about this month!")
29
30. @DylanSeychell
Using FOR to generate numbers
The FOR construct can be used like the for in C-style languages.
for i in range(4):
print (i)
The range generates numbers from 0 up until the number passed as a parameter
and not including this last number.
Range can also accept a start and end parameter as follows: range(2,4)
30