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

EDU4SDS Lecture 1 Python Intro

This document provides an introduction to Python basics, including data types, containers, functions, conditionals, loops, classes, and array/matrix operations. It covers fundamental Python concepts like integers, floats, strings, booleans, variables, lists, tuples, dictionaries, built-in and custom functions, if/else statements, while and for loops. It also demonstrates defining classes, inheritance, and using the NumPy library to perform operations on arrays and matrices.

Uploaded by

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

EDU4SDS Lecture 1 Python Intro

This document provides an introduction to Python basics, including data types, containers, functions, conditionals, loops, classes, and array/matrix operations. It covers fundamental Python concepts like integers, floats, strings, booleans, variables, lists, tuples, dictionaries, built-in and custom functions, if/else statements, while and for loops. It also demonstrates defining classes, inheritance, and using the NumPy library to perform operations on arrays and matrices.

Uploaded by

Ebuwa Amadasun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Python Basics 1

Adit Murali
University of Strasbourg
1 Introduction

Adit Murali, PhD Candidate Prof. Nicolas Padoy


University of Strasbourg CAMMA
Surgical Video Analysis University of Strasbourg
2021 – Today IHU Strasbourg

Python Basics 1
2 Goals

• Python Fundamentals
• Numbers, Logic, Data Types
• Functions
• Conditionals
• Loops
• Classes
• Array/Matrix Operations
• Numpy

Python Basics 1
3 Data Types & Containers

• All forms of data can be categorized into a particular data type


• Integers (int): 1, 37, -58, …
• Floating Point Numbers (float): 3.8, -12.5, 7.0, …
• Strings (str): "hello", "czhds", "catch-22", "87"
• Defined using quotation marks
• Booleans (bool): True, False
• We can define variables to hold data
• my_var = "hello"
•a = 7
• ...and manipulate them
• b = a + 10 (b now has the value 17)

Python Basics 1
4 Data Types & Containers

• These data can be grouped through various types of containers


• Lists
• my_list = ["hello", 1, 7.0], …
• Can modify/add/remove elements as you wish
• Tuples
• my_tuple = ("this", "tuple", "has", 4, "elements")
• Cannot modify/add/remove elements
• Dictionaries
• Store data as key-value pairs - {key1: value1, key2: value2}
• Keys and values can both be any data type
• Values are accessed using the key
• my_dict = {"a": 10, 2: "hello", "my_key": -7.8}
• Can add/remove/modify elements

Python Basics 1
5 Data Types & Containers

• Some examples!

Python Basics 1
6 Functions

• Functions do stuff!
• Examples:
• print("Hello World!")
• sum([1, 4, 7.8, -10])
• Take inputs (values inside the parentheses), and give one
or more outputs
• Built-In & Custom

Python Basics 1
7 Conditionals and Loops

• Conditional statements allow a single python program to


do different tasks depending on the situation
• if conditionA do somethingA else if conditionB do so
methingB else do somethingC
• Loops allow repeating a certain action or series of
actions N times
• while conditionA do something
• for a = 0...10 do something
• for a in ["a", 37, 2.7, "hello"] do something

Python Basics 1
8 Functions + Conditionals and Loops

• Some examples!

Python Basics 1
9 Classes

• Just like functions do stuff, classes are stuff


• E.g. could create a dog class or a cat class
• Once a class is defined, you can create instances of a class
• To define a class:
• Create __init__ function, which is run when you create an instance
• Bob = Dog("Bob")
• We can access the attributes of a class
• Bob = Dog("Bob", 15)
• Bob.name, Bob.age

Python Basics 1
10 Classes

• Basic Example

Python Basics 1
11 Classes

• We use the self keyword when we want to refer to attributes


of a class when defining the class or it's member functions
• Inheritance
• Can also create classes that are subclasses of others
• e.g. class GoldenRetriever(Dog)
• Inherit all attributes, __init__, and member functions

Python Basics 1
12 Array and Matrix Operations

• Machine learning often involves mathematical operations


on matrices
• We use numpy to handle such operations
• import numpy as np
• Countless other external libraries
• import libraryx as libx

Python Basics 1
13 Array and Matrix Operations

• Create array (2D, 3D, … all called array)


• M_0 = np.zeros([10, 10])
• M_rand = np.random.randn(10, 10)
• Check shape
• M_0.shape
• Add/Subtract
• M_0 + M_rand, M_0 – M_rand
• Multiply
• Element-Wise: np.multiply(M_0, M_rand)
• Matrix Multiply: np.dot(M_0, M_rand)
• Reshape
• M.reshape(5, 20)

Python Basics 1
14 Array and Matrix Operations

• Some examples!

Python Basics 1

You might also like