0% found this document useful (0 votes)
10 views26 pages

Lecture_Intro_Python

This is a detailed lecture into python and it's basics

Uploaded by

dareeshagalani
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)
10 views26 pages

Lecture_Intro_Python

This is a detailed lecture into python and it's basics

Uploaded by

dareeshagalani
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/ 26

Programming in Python

Dr. Sajid Ghuffar


sajid.ghuffar@grel.ist.edu.pk

Department of Space Science


Institute of Space Technology
Outline
• Python Basics
 Lists, Tuples, Dictionaries, Arrays,
 Loops, Conditional statements
 Functions
 Exception/Error Handling

• Object Oriented Programming in Python


 Classes, Objects, Inheritance

• Data processing and Visualization


 Numpy Arrays, Pandas
 Matplotlib, Seaborn

2
Python Books /Literature
 Severance, C. R., Blumenberg, S., & Hauser, E. (2016). Python for Everybody:
Exploring Data in Python 3.
 Shaw, Z. A. (2013). Learn Python the hard way: A very simple introduction
to the terrifyingly beautiful world of computers and code. (Third, Ed.)
Addison-Wesley.
 Downey, A. (2012). Think Python. O'Reilly Media, Inc.
 Lutz, M. (2013). Learning python: Powerful Object-Oriented Programming.
O'Reilly Media, Inc.
 Swaroop, H. C. (2013). A Byte of Python. Independent.
 https://docs.python.org/3/tutorial/
 https://www.w3schools.com/python/
 https://www.tutorialspoint.com/python/index.htm

3
Python Introduction

4
Introduction to Python
• Python programming language was started by Guido van Rossum in the
late 1980s. He started its implementation in December 1989 and released
the first public version, Python 0.9.0, in February 1991.
• High-Level, Interpreted Language: Python is an interpreted language,
which means it doesn't need to be compiled before it's run. Its syntax is
clean and easy to read, making it a favorite for beginners and experienced
developers alike.
• Versatile and Powerful: Used in a variety of applications, from web
development (Django, Flask) to data analysis (Pandas, NumPy) to artificial
intelligence (TensorFlow, PyTorch) and more.
• Vast Standard Library: Python comes with a comprehensive standard
library that includes modules and packages for a wide range of tasks,
reducing the need for external libraries.
• Dynamic and Strongly Typed: Python uses dynamic typing, meaning that
variable types are determined at runtime. However, it's also strongly typed
because types cannot be changed once they’re set, and operations on
incompatible types raise errors.
5
Main Python Versions
• Python 1.0 (January 26, 1994): The first official version. It already
included many features familiar to users today, like exception handling and
functions.

• Python 2.0 (October 16, 2000): Introduced new features such as list
comprehensions and a garbage collection system. Python 2.7, released on
July 3, 2010, was the last major release in the 2.x series and was supported
until January 1, 2020.

• Python 3.0 (December 3, 2008): A major, backwards-incompatible release.


It addressed and rectified fundamental design flaws in the language. Some
key changes included the "print" statement becoming a function, integer
division producing a float instead of an integer, and a new bytes type for
handling binary data.
 Current version is 3.11 (June 2023)

6
Main Python Libraries

7
Python Installation: Anaconda
 Anaconda is a popular framework for installing Python
 It makes it easy to search and install thousands of Python/R packages
and access a vast library of community content and support.

https://www.anaconda.com/download 8
Python Installation: Miniconda
 Miniconda is a minimal conda installer. So, we need to install almost all
libraries and tools like Spyder etc ourselves trhough pip or conda
commands.

https://docs.conda.io/projects/miniconda/en/latest/index.html 9
Python Installation: Anaconda Prompt commands

10
Python IDEs: Spyder

https://docs.spyder-ide.org/current/index.html
11
Spyder: Changing to a different python version
 If you want to change to different versions of Python in Spyder. Go to
tools -> preferences and change the Python interpreter to a different
version
 You may have to install sypder kernels in the new environment

https://docs.spyder-ide.org/current/index.html
12
Python IDEs: PyCharm

https://www.jetbrains.com/pycharm/
13
Python: Jupyter Notebook

The Jupyter Notebook is a web-based interactive computing platform. The


notebook combines live code, equations, narrative text, visualizations

https://jupyter.org/ 14
Google Colab

We can also use Earth Engine Python API in Colab


https://colab.research.google.com/notebooks/welcome.ipynb 15
Main Python Data Types
 Numeric Types:
• int: Integer type, e.g. 5
• float: Floating-point type, e.g. 5.0
• complex: Complex number type, e.g. 5 + 3j
 Sequence Types:
• str: String type, e.g. "Hello, World!"
• list: List type, e.g. [1, 2, 3]
• tuple: Tuple type, e.g. (1, 2, 3)
 Mapping Types:
• dict: Dictionary type, e.g. {"key": "value"}
 Boolean Type:
• bool: Boolean type, e.g. True or False

16
Python Documentation

https://docs.python.org/

17
Keywords
Python: Lists
Lists are mutable ordered sequences of elements.
 Diverse Elements: Can contain elements of different types.
 Syntax: Defined using square brackets [].
 Mutable: Elements can be added, removed, or changed after the list is
created.
 Methods: Provides a variety of methods to manipulate the list (e.g., append,
extend, remove, etc.).

# Creating an empty list


my_list = []
# Creating a list with elements
my_list = [1, 2, 3]
# Computing the length (size/shape) of
the list
length_of_list = len(my_list)

21
Python: Lists
# Appending an element to the end of the list
my_list.append(4)
# Inserting an element at a specific index
my_list.insert(2, 2.5)
# Modifying an element at a specific index
my_list[1] = 1.5
# Adding elements from another list
my_list.extend([10, 20, 30])
# Removing a specific element by value
my_list.pop(2)
# Creating a 2-D list (list of lists)
list2D = [ [1, 2, 3], [4, 5, 6]]
# Computing the number of rows (outer length)
num_rows = len(list2D)
# Computing the number of columns
num_cols = len(list2D[0]) if num_rows > 0 else 0
# indexing certain element of the array e.g. 2nd row , 3rd column
val=list2D[1][2]

22
Python: Tuples
Tuples are immutable ordered sequences of elements.
 Diverse Elements: Can contain elements of different types.
 Syntax: Defined using parentheses ().
 Immutable: Once created, elements cannot be added, removed, or modified.
 Use Case: Suitable for read-only data, and can be used as keys in
dictionaries.
# Creating an empty tuple
my_tuple = ()
# Creating a tuple with elements
my_tuple = (1, 2, 3)
# Computing the length (size/shape) of the tuple
length_of_tuple = len(my_tuple)
# Since tuples are immutable, we cannot append, insert or modify elements directly.
# However, we can concatenate tuples to create a new tuple
my_tuple = my_tuple + (4,)

23
Python: Tuples
# Similarly, inserting an element at a specific index involves creating a new tuple
my_tuple = my_tuple[:2] + (2.5,) + my_tuple[2:]
# Modifying an element at a specific index also involves creating a new tuple
my_tuple = my_tuple[:1] + (1.5,) + my_tuple[2:]
# Adding elements from another tuple
my_tuple = my_tuple + (10, 20, 30)
# Removing a specific element by value is not directly possible with tuples
# But we can achieve this by converting it to a list or using tuple comprehension
my_tuple = tuple(x for x in my_tuple if x != 2.5)
# Creating a 2-D tuple (tuple of tuples)
tuple2D = ((1, 2, 3), (4, 5, 6))
# Computing the number of rows (outer length)
num_rows = len(tuple2D)
# Computing the number of columns
num_cols = len(tuple2D[0]) if num_rows > 0 else 0
# Indexing certain elements of the array, e.g., 2nd row, 3rd column
val = tuple2D[1][2]

24
Python: Dictionaries
Dictionaries are mutable unordered collections of key-value pairs.
 Unique Keys: Each key must be unique and immutable (e.g., strings,
numbers, tuples).
 Mutable Values: The values associated with keys can be of any type and are
mutable.
 Syntax: Defined using curly braces {}.
 Access: Elements are accessed using keys, not indices.
 Methods: Provide methods to manipulate dictionaries (e.g., keys, values,
items, update, etc.).
# Creating an empty dictionary
people_dict = {}
# Creating a dictionary with some key-value pairs
people_dict = { 'John': {'age': 30, 'country': 'USA'},
'Maria': {'age': 25, 'country': 'Spain'},
'Yuki': {'age': 35, 'country': 'Japan'} }

25
Python: Dictionaries
# Computing the length (size) of the dictionary
length_of_dict = len(people_dict)
# Adding a new key-value pair to the dictionary
people_dict[‘Liu'] = {'age': 40, 'country': ‘China'}
# Modifying the value associated with a specific key
people_dict['John']['age'] = 32
# Adding key-value pairs from another dictionary
new_people = {
'Olivia': {'age': 28, 'country': 'Australia'},
'Sophie': {'age': 22, 'country': 'Canada'}
}
people_dict.update(new_people)
# Removing a specific key-value pair by key and getting its value
removed_person = people_dict.pop('Yuki')

26

You might also like