This document is an introduction to Python programming presented in two parts. Part 1 covers basic Python concepts like data types, strings, lists, dictionaries, if/for statements and writing simple programs to print messages. It provides examples and encourages the reader to write their own small programs. The learning outcomes are listed as understanding Python basics, different statement types and completing simple programs. This introduction aims to teach Python fundamentals in an accessible way through examples and hands-on exercises.
2. Silvia Tinena Coris
Table of contents
Part 1:
1. Learning Outcomes
2. Introduction
3. The first program: hello world!
4. The second program: hello (your name)!
5. More data types
6. Important things to know
7. If Statement
8. The third program: boy or girl?
9. For Statement
10. The fourth and fifth programs: your name + hello
students!
2
3. Silvia Tinena Coris
Table of contents
Part 2:
11. While Statement
12. The sixth program: security word
13. Functions
14. Seventh program: my functions
15. Interesting links
16. Exercises
3
5. Silvia Tinena Coris
Learning Outcomes
State the basis of Python programming
Discover the data types
Learn the different types of statements
Do some simple programs
5
7. Silvia Tinena Coris
Why Python?
7
Easy to learn
Powerful programming language
Efficient high-level data structures
Simple but effective approach to object-
oriented programming
Elegant syntax and dynamic typing
8. Silvia Tinena Coris
How to use Python?
8
From the shell or from the saved programs.
The shell executes each instruction as you type it
Useful for experiments
Each line starts with three chevron: >>>
The saved programs are executed all at once.
If something isn’t correct, an error line will appear
in red explaining which kind of mistake it is and
where it is
9. Silvia Tinena Coris
How to get Python?
From a PC, downloadable for free in the
official website:
https://www.python.org/downloads/
9
10. Silvia Tinena Coris
How to get Python?
From Raspberry Pi, it is already downloaded
and ready to use in the desktop (IDLE or IDLE
3)
10
12. Silvia Tinena Coris
Numbers
12
int: signed integer numbers
long: long integers (can also be represented in
octal or hex)
float: real numbers, fractional part, floating point
complex: complex numbers (j for the imaginary
part)
13. Silvia Tinena Coris
Numbers
13
0x indicates hexadecimal
L, l indicates long integer
It is better to write an uppercase L to avoid
confusion with the number 1
15. Silvia Tinena Coris
First steps
Open the IDLE
File / New File
Write your code on the new window!
Save it
Save before running the program (ctrl + s)
If not, it will ask you to save it. Always say yes!
To run: Run / Run Module or F5
Be careful!!! Python is case
sensitive
To interrupt the program: ctrl+c
15
19. Silvia Tinena Coris
Hello (your name)!
19
Now, try it yourself!
Create a questionnaire and swap places with
your classmates to see who has the best one
21. Silvia Tinena Coris
Strings
21
Contiguous set of characters in between quotation marks,
which can be simple (‘ ‘) or double (“ “)
Accessing a single positing of a string:
String[n]
Where n is the desired position
Accessing more than one position in a string:
String[0:2]
Take the positions 0 and 1 of the string
The first number is included
The second isn’t!!
Remember: the first position is included, the last isn’t
22. Silvia Tinena Coris
Strings
Accessing more than one position in a string:
String[:2]
Take the first two positions
If there is no number 0, from the beginning
String[2:]
Take from the second position to the end
If there is no number the end
+ concatenation
* repetition
22
24. Silvia Tinena Coris
Lists
24
Enclosed within square brackets ([ ])
Separated by commas (,)
It is possible to have different types of data
within the same string
They can be accessed as the strings
26. Silvia Tinena Coris
Tuples
26
Similar to lists, but tuples CAN’T BE
MODIFIED
“Only-read list”
Enclosed within parentheses ( ( ) )
Separated by commas
28. Silvia Tinena Coris
Modifying Lists
28
Now, try it yourself!
Take the lists that you’ve just created
and modify some elements
29. Silvia Tinena Coris
Dictionary
29
Enclosed within curly brackets ({ })
Separated by commas (,)
It is possible to have different types of data
within the same dictionary
Function (variable) in (dictionary)
It tells us whether the variable is in the dictionary
(True) or not (False)
31. Silvia Tinena Coris
Dictionary – level 2
31
Now, try it yourself!
Create your own
personalised messages for
checking the items on your
shopping list
32. Silvia Tinena Coris
Dictionary – level 3
32
Now, try it
yourself!
Swap places with
your classmates
and see who
gets the highest
score
33. Silvia Tinena Coris
Converting types
33
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 [,imag]) Creates a complex number.
str(x) Converts object x to a string representation.
tuple(s) Converts s to a tuple.
list(s) Converts s to a list.
hex(x) Converts an integer to a hexadecimal string.
oct(x) Converts an integer to an octal string.
34. Silvia Tinena Coris
Calculator
34
+
- will return the same type of number
* if one is float, then the result will be float
/ will always return a float (decimals)
// forces a floor division (no decimals) – div
% calculates the remainder – mod
37. Silvia Tinena Coris
Important things to know
37
Declaration:=
Used to define the value of a variable or a constant
Comparison: ==
Used to compare one variable or constant with a value
It appears in statements such as if and while
print (…) parentheses needed!
len(string) it gives you the length of a string
help(topic) it opens the help menu about the topic
No need for semicolon at the end of the sentences!
Key words in lowercase!
38. Silvia Tinena Coris
Important things to know
38
Identifiers:
Must start with a letter or an underscore
Punctuation characters, @, $, £, %, etc. are not
allowed
n
It creates a line break
You can write as many as you want together to create
more than one blank line.
eg. nnn would create 3 blank lines
True and False (Boolean) must have the first letter
upper-case! If so, they should be red (automatic)
39. Silvia Tinena Coris
Important things to know
39
If we don’t want to end the program until the
user has pressed the enter key:
input(‘Press the enter key to exit’)
(Yes, it has to be the enter key)
If you are printing strings and numbers in the
same line (the same print statement), you
should convert the numbers to string using the
function str()
print(‘Your favourite number is
‘+str(favnumber))
40. Silvia Tinena Coris
Important things to know
40
If we need that a string occupies more than
one line, start it with triple quotes (‘‘‘ ’’’) or (“““
”””)
If a string has to contain an apostrophe (‘), it
should be written with a backslash before it to
avoid Python interpreting it as the end of the
string
eg. print(‘They aren’t studying hard
enough.)
41. Silvia Tinena Coris
Coding Styles
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 spaces around operators and after commas,
but not directly inside bracketing constructs:
a = f(1, 2) + g(3, 4)
41
42. Silvia Tinena Coris
Common Mistakes
42
Double check that:
The indentation of all loops is correct
The blank spaces ( ) are where they should be
Extra spaces may lead into errors
All brackets are closed
When editing the code, whichever is enclosed in the
brackets will be momentary highlighted in grey
You have colons (:) after statement words
The spelling of the variables, functions and
constants is the same throughout all the code
Remember that Python is very sensitive!
46. Silvia Tinena Coris
Boy or girl? – version 1
We need to convert the input to an integer!
46
Now, try it yourself!
Create your own personalised
messages and options
48. Silvia Tinena Coris
Boy or girl? – version 2
48
Now, try it yourself!
Create your own
personalised messages
49. Silvia Tinena Coris
Boy or girl? – version 2.2
49
Now, try it yourself!
Create your own personalised messages
with different options and different
questions
51. Silvia Tinena Coris
For
51
In other programming languages, for is used
to iterate a sequence over certain conditions
defined by the user… but not in Python!
In Python, it iterates over the items of any
sequence (a list or a string), in the order that
they appear in the sequence.
for (i) in (string):
…
52. Silvia Tinena Coris
For
52
Now, try it yourself!
Create your own list and
iterate on it to print all of its
elements on separate lines
Why not trying adding
something after each word?
53. Silvia Tinena Coris
Range
53
Is like the for function in other programming
languages
for (i) in range(n):
…
Remember that Python works with 0 as
the first element! Now, try it
yourself!
54. 10. Fourth and fifth programs:
your name + hello students
54
55. Silvia Tinena Coris
Your name in different lines
55
Now, try it yourself!
Create your own question
and iterate it
56. Silvia Tinena Coris
Hello students
56
Now, try it yourself!
Create your own list and
iterate on it to print all of its
elements on separate lines
with a message