Intro-Python-1
Intro-Python-1
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 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
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?
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
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
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!
F Strings
• A new string formatting mechanism known as Literal
String Interpolation or more commonly as F-strings
name = 'Fahad'
age = 23
print(f"Hello, My name is {name} and I'm {age} years old.")
my_function(“Fahad", “Satti")
my_function(“NUST")
What is a Virtual Environment?
40
https://www.jetbrains.com/pycharm/download/
#section=windows
Ipython Configuration
File->SettingProject :[project name]Python
Interpreter ipython install
Example
Sample.py