Use a locally installed editor like Spyder or Visual Studio Code. If you want to try out coding without installing anything, try this online editor: https://www.pythonanywhere.com/try-ipython/
In order to explain the code, comments in Python can be made by preceding a line with a hashtag #
. Everything on that particular line will then not be interpreted as part of the code:
# This is a comment and will not be interpreted as code
The basic data types in Python are illustrated below.
int
)¶# Integers
a = 2
b = 239
float
)¶# Floats
c = 2.1
d = 239.0
str
)¶e = 'Hello world!'
my_text = 'This is my text'
Both "
and '
can be used to denote strings. If the apostrophe character should be part of the string, use "
as outer boundaries:
"Barack's last name is Obama"
Alternatively, \
can be used as an escape character:
'Barack\'s last name is Obama'
"Barack's last name is Obama"
Note that strings are *immutable*, which means that they can't be changed after creation. They can be copied, then manipulated and thereafter saved into a new variable though.
bool
)¶x = True
y = False
As you might have noticed, Python does not require you to declare the type of a variable before creating it. This is because Python is a dynamically typed language.
To create a variable a
in a statically typed language, it would go something like (C++):
int a a = 5
You might also have seen this when using VBA with Option Explicit enabled, where it would be Dim a As Integer
and then a = 5
. If the variable type is not declared beforehand in such languages, an error will be thrown.
This is not the case in Python. It automatically figures out which type to assign to each variable in the background.
This does not mean that Python is 'smarter' than other languages, it's purely an implementation choice from the author of the language to make it behave this way. And it has both advantages and drawbacks.
Dynamic typing also means that variables can change types throughout the program, so somthing like this is valid:
a = 5 a = 'Hi'
Which can be both a blessing and a curse. The flexibility is nice, but it can lead to unexpected code behavior if variables are not tracked.
Most basic operations on integers and floats such as addition, subtraction, multiplication work as one would expect:
2 * 4
8
2 / 5
0.4
3.1 + 7.4
10.5
Exponents are denoted by **
:
2**3
8
Watch Out: The
^
operator is not used for exponentiation. Instead, it's aBinary XOR operator
(whatever that is).
Floor division is denoted by //
. It returns the integer part of a division result (removes decimals after division):
10 // 3
3
Modulo is denoted by %
. It returns the remainder after a division:
10 % 3
1
Strings can be added (concatenated) by use of the addition operator +
:
'Bruce' + ' ' + 'Wayne'
'Bruce Wayne'
Multiplication is also allowed:
'a' * 3
'aaa'
Subtraction and division are not allowed:
'a' / 3 # Division results in error
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-14-18a27a45574d> in <module> ----> 1 'a' / 3 # Division results in error TypeError: unsupported operand type(s) for /: 'str' and 'int'
'a' - 'b' # Subtraction results in error
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-15-f52d61c1bb56> in <module> ----> 1 'a' - 'b' # Subtraction results in error TypeError: unsupported operand type(s) for -: 'str' and 'str'
There is quite often a need for printing a combination of static text and variables. This could e.g. be to output the result of a computation. Often the best way is to use the so-called f-strings. See examples below.
# Basic usage of f-strings
a = 2
b = 27
print(f'Multiplication: a * b = {a} * {b} = {a*b}')
print(f'Division: a / b = {a} / {b} = {a/b}')
Multiplication: a * b = 2 * 27 = 54 Division: a / b = 2 / 27 = 0.07407407407407407
# f-strings with formatting for number of decimal places
print(f'Division: a / b = {a} / {b} = {a/b:.3f}') # The part ':.xf' specfifies 'x' decimals to be printed
Division: a / b = 2 / 27 = 0.074
Both variables and complex computations can be inserted inside the curly brackets to be printed.
print(f'Computation insde curly bracket: {122**2 / 47}')
Computation insde curly bracket: 316.6808510638298
len()
¶The len()
function returns the length of a sequence, e.g. a string:
len('aaa')
3
len('a and b') # Spaces are also counted
7
A string object can be interacted with in many ways by so-called *methods*. Some useful methods are shown below:
name = 'Edward Snowden'
string.replace()
¶Replaces characters inside a string:
string.replace('old_substring', 'new_substring')
name.replace('Edward', 'Ed')
'Ed Snowden'
Recall that strings are *immutable*. They can be copied, manipulated and saved to a new variable. But they can't be changed per se. This concept transfers to other more complex constructs as well.
Thus, the original string called name
is still the same:
name
'Edward Snowden'
In order to save the replacement and retain the name of the variable, we could just reassign it to the same name:
name = name.replace('Edward', 'Ed')
Internally, the computer gives this new name
variable a new id due to the immutability, but for us this does not really matter.
string.endswith()
¶This method might be self explanatory, but returns a *boolean* (True
of False
) depending on whether or not the strings ends with the specified substring.
string.endswith('substring_to_test_for')
name.endswith('g')
False
name.endswith('n')
True
name.endswith('den')
True
string.count()
¶Counts the number of occurences of a substring inside a string:
string.count('substring_to_count')
text = 'This is how it is done'
text.count('i')
4
text.count('is')
3
The match is case sensitive:
text.count('t')
1
In Python, code blocks are separated by use of indentation. See the defintion of an if
-statement below:
if condition:
# Code goes here (must be indented!)
# Otherwise, IndentationError will be thrown
# Code placed here is outside of the if-statement
Where evaluation of condition
must return a boolean (True
or False
).
Remember:
- The
:
*must* be present aftercondition
.- The line immediately after
:
*must* be indented.- The
if
-statement is *exited by reverting the indentation* as shown above.
This is how Python interprets the code as a block.
The same indentation rules are required for all types of code blocks, the if
-block above is just an example. Examples of other types of code blocks are for
and while
loops, functions etc.
All editors will automatically make the indentation upon hitting enter after the :
, so it doesn't take long to get used to this.
In many other programming languages indentation is not required. It is however still used as good practice to increase code readability. Instead of indentation, code blocks are denoted by encapsulating code in characters like ()
, {}
etc.
x = 2
if x > 1:
print('x is larger than 1')
x is larger than 1
if
/ else
-statements¶y = 1
if y > 1:
print('y is larger than 1')
else:
print('y is less than or equal to 1')
y is less than or equal to 1
if
/ elif
/ else
¶z = 0
if z > 1:
print('z is larger than 1')
elif z < 1:
print('z is less than 1')
else:
print('z is equal to 1')
z is less than 1
An unlimited number of elif
blocks can be used in between if
and else
.
Find the length of the following string:
s = "Batman's real name is Bruce Wayne"
Test if s
from above has "Wayne" as last characters (should return True
of course)
Print the following sentence using an f-string
:
'The string s has a length of {insert_length_of_s} items.'
Use s
from above.
Use the count()
method to print the number of *e*'s in the string s
form above.
Use the replace()
method to replace Ø
with Y
in the following string:
string1 = '33Ø12'
Save the new string in a variable string2
and print the following sentence:
'The string {insert_string1} was replaced by {insert_string2}'
If the string below has more than 100 characters, print "String has more than 100 characters", otherwise print "String has less than or exactly 100 characters".
dummy_string = 'Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.'
Print the number of space characters in dummy_string
from above.
Create the variables
letter1 = 'e'
letter2 = 'm'
Convert this pseudo code to a Python program:
if there are more occurrences of letter1 than letter2 in dummy_string:
print("There are more {insert_letter1}'s than {insert_letter2}'s")
elif there are more occurrences of letter2 than letter1 in dummy_string:
print("There are more {insert_letter2}'s than {insert_letter1}'s")
else:
print("There are exactly the same number {insert_letter1}'s and {insert_letter2}'s")
Test the program you wrote above with different combinations of letters for the variables letter1
and letter2
.
If you are still with us at this point you can try to implement a print message of the actual number of occurrences.
The cell below is for setting the style of this document. It's not part of the exercises.
from IPython.display import HTML
HTML('<style>{}</style>'.format(open('../css/cowi.css').read()))