Introduction to
Ask!
The art and science of asking questions is the source of all knowledge.
- Thomas Berger
• Do not hesitate to ask!
• If something is not clear, stop me and ask.
• During exercises (you can also ask others).
Image by mohamed Hassan from Pixabay
Now let me ask something..
• Why do you want to learn Python/programming?
• What would you use Python for?
Failure
• Coding is all about trial and error.
• Don't be afraid of it.
• Error messages aren't scary, they are useful.
Python natalensis by A. Smith on Wikimedia Commons
History
• Started by Guido Van Rossum as a hobby Guido Van Rossum
by Doc Searls
• Now widely spread on Flickr CC-BY-SA
• Open Source! Free!
• Versatile
• https://spectrum.ieee.org/the-top-programming-languages-2
023
Python today
• Developed a large and active scientific computing and data analysis
community
• Now one of the most important languages for
• Data science
• Machine learning
• General software development
• Packages: NumPy, pandas, matplotlib, SciPy, scikit-learn, statsmodels
Time for a demo..
https://www.youtube.com/watch?v=BBwEF6WBUQs
Python as a calculator
• Let us calculate the distance between Edinburgh and London in km
Variables
• Great calculator but how can we make it store values?
• Do this by defining variables
• Can later be called by the variable name
• Variable names are case sensitive and unique
We can now reuse the variable mileToKm in the next block without
having to define it again!
Types
Variables actually have a type, which defines the way it is stored.
The basic types are:
Why should we care?
Image by Clker-Free-Vector-Images on Pixabay
Important lesson to remember!
We can't do arithmetic operations on variables of different types. Therefore make sure
that you are always aware of your variables types!
You can find the type of a variable using type(). For example type type(x).
Casting types
Luckily Python offers us a way of converting variables to different types!
Casting – the operation of converting a variable to a different type
Similar methods exist for other
data types: int(), float(), str()
Quick quiz
What will be the result?
Arithmetic operations
Similar to actual Mathematics.
Order of precedence is the same as
in Mathematics.
We can also use parenthesis ()
Order precedence example
Quick quiz
vs
13 49
Comparison operators
• I.e. comparison operators
• Return Boolean values
(i.e. True or False)
• Used extensively for
conditional statements
Comparison examples
False
Logical operators
• Allows us to extend the conditional logic
• Will become essential later on
Combining both
True True
Another example
True True True
That wasn't very easy to read was it?
Is there a way we can make it more readable?
Strings
• Powerful and flexible in Python
• Can be added
• Can be multiplied
• Can be multiple lines
Strings
Strings
These are called methods and add extra functionality to the String.
If you want to see more methods that can be applied to a string simply
type in dir('str')
Mixing up strings and numbers
Often we would need to mix up numbers and strings.
It is best to keep numbers as numbers (i.e. int or float)
and cast them to strings whenever we need them as a string.
Multiline strings
Printing
• When writing scripts, your outcomes aren't printed on the terminal.
• Thus, you must print them yourself with the print() function.
• Beware to not mix up the different type of variables!
Quick quiz
Do you see anything wrong with this block?
Another more generic way to fix it
If we comma separate statements in a print function we can
have different variables printing!
Placeholders
• A way to interleave numbers is
• Elegant and easy
• more in your notes
Commenting
• Useful when your code needs further explanation. Either for your
future self and anybody else.
• Useful when you want to remove the code from execution but not
permanently
• Comments in Python are done with #
Exercise time
Simple and fun exercises.
Failure is progress!
Ask us anything. Ask among yourselves as well.