Intro Python 1
Intro Python 1
Part 1
History
• Started by Guido Van Guido Van Rossum
Rossum as a hobby by Doc Searls
on Flickr CC-BY-SA
2. Python scripts
What if we want to run more than a few lines of code?
Then we must write text files in .py
Time for a demo..
https://www.youtube.com/watch?v=BBwEF6WBUQs
Noteable (Jupyter notebooks)
• Easy to use environment
• Web-based
• Combines both text and code
into one
• Come with a great number of
useful packages
1. Start Noteable
Agenda
• Variables
• Types
• Arithmetic operators
• Boolean logic
• Strings
• Printing
• Exercises
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:
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
False
Logical operators
• Allows us to extend the conditional logic
• Will become essential later on
Combining both
True True
Another example
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!