0% found this document useful (0 votes)
3 views

Intro Python 1

Uploaded by

usman6480463
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Intro Python 1

Uploaded by

usman6480463
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Introduction to

Part 1
History
• Started by Guido Van Guido Van Rossum
Rossum as a hobby by Doc Searls
on Flickr CC-BY-SA

• Now widely spread


• Open Source! Free!
• Versatile
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
2 Modes
1. IPython
Python can be run interactively
Used extensively in research

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

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 ()


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!

You might also like