0% found this document useful (0 votes)
6 views46 pages

Intro-Python-1

Uploaded by

ahmedpandit48
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
6 views46 pages

Intro-Python-1

Uploaded by

ahmedpandit48
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 46

Introduction to

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?

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!
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.")

Hello, My name is Fahad and I'm 23 years old.


Quick quiz
Do you see anything wrong with this block?
str1 = "which means it has even more than"
str2 = 76
str3 = "quirks"
print(str1+str2+str3)
Another more generic way to fix it
str1 = "which means it has even more than"
str2 = 76
str3 = "quirks"
print(str1,str2,str3)

which means it has even more than 76 quirks

If we comma separate statements in a print function we can


have different variables printing!
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 #
Conditions
• Pretty much same as c++/java etc
• Only difference is that instead of { and } indentation is used. Python always
uses indentation instead of { } to show a block should run together
• Else ifelsif
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
Loops
• fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)
Range
• range() is a type and a function
• returns a sequence of numbers, starting from 0 by
default, and increments by 1 (by default), and ends at a
specified number.
• The range() function defaults to 0 as a starting value,
however
• specify the starting value
• And end value by adding a parameter: range(2, 6), which
means values from 2 to 6 (but not including 6)
• A third argument will tell the step size range(100,205,3)
Range as a Type
• represents a range of numbers without generating the entire
sequence in memory.
• is an iterable and can be used in for loops and other iterable contexts.
• does not support indexing or item assignment because it doesn't
actually store the sequence in memory.
r = range(5) range(0, 5)
print(r) <class 'range'>
print(type(r)) 0
for num in r: 1
print(num) 2
3
4
Range as a Function
• returns a list-like sequence of numbers based on the specified range.
• It is used when you need to work with the actual sequence of
numbers and require indexing or slicing.
• It is often combined with the list() function to convert the range
object to a list.
seq = list(range(5)) [0, 1, 2, 3, 4]
print(seq)
Functions
def my_function(fname, lname=“SEECS"):
print(fname + " " + lname)

my_function(“Fahad", “Satti")
my_function(“NUST")
What is a Virtual Environment?

• An environment for your user account


• Install python packages without adversely affecting other users

• Use different sets of packages for different projects


 Project A requires Pandas 1.0
 Project B requires Pandas 0.25
 Solution ... create 2 separate virtual environments

40

Wharton Research Data Services


Virtual Environment
Terminology
• Conda: open source package management
system and environment management system
• Pip: command to install python libraries in a
virtual environment
• Command line
• Pip install opencv-python
Pycharm

https://www.jetbrains.com/pycharm/download/
#section=windows
Ipython Configuration
File->SettingProject :[project name]Python
Interpreter ipython install
Example
Sample.py

You might also like