Tema 1_Introduction Python
Tema 1_Introduction Python
INTRODUCTION TO
PYTHON
Máster Universitario en Mecánica de Fluidos Computacional
LEARNING OUTCOMES
• Describe the syntax of Python
• Employ conditional structures in Python to branch the
behavior of a software
• Employ looping structures in Python to repeat sequences or
blocks of actions
• Define functions in Python to encapsulate and organize code
• Describe the behavior of different types of data structures
• Employ appropriate data structures to create effective software
OPT 2
UPV
ASSUMPTIONS
• You have some patience and resilience to deal with bugs and
unexpected errors ☺ OPT 3
UPV
VARIABLES, DATA
TYPES AND Basic foundations
OPERATORS
OPT 4
UPV
BASIC DATA TYPES AND VARIABLES
OPT 6
UPV
COMPARISONS AND BOOLEAN OPERATORS
OPT 7
UPV
CONDITIONAL Branching the logic of
your code
STRUCTURES
OPT 8
UPV
CONDITIONAL STRUCTURES
if ... elif ... else
• In Python, we can use the if ... elif ... Else print("I am a number between 11 and 20")
structure else :
• We don't use { } for delimiting code x = x**2
blocks. Indentation defines code blocks. print("Before, I was less than or equal to 10,
• We can have nested conditional structures but now I am " + str(x))
OPT 9
UPV
CONDITIONAL STRUCTURES
Match - Case
• From Python 3.1 onwards, they lang = input("What's the programming language you want to
introduced match and case learn? ")
OPT 10
UPV
LOOPING Repeating code blocks
STRUCTURES
OPT 11
UPV
LOOPING STRUCTURES
for
• Types of looping:
• Known number of times
• Unknown number of times
• for loops are typically used when we
know beforehand how many times we
want to repeat a block of instructions
• A given number of times (i.e., n)
• For each element in a given data structure
• We can use range to iterate using a
typical for structure found in other
programming languages
• No {} and indentation is a must
OPT 12
UPV
LOOPING STRUCTURES
for
OPT 13
UPV
LOOPING STRUCTURES
while
OPT 14
UPV
LOOPING STRUCTURES
while
OPT 15
UPV
Create code recipes
FUNCTIONS
OPT 16
UPV
FUNCTIONS
Creating code recipes
• Functions allow us to define code blocks def normalize(value, mu, sigma): #This defines the
that will be used in several parts of the function or code recipe
code. They are code recipes.
#value, mu, and sigma represent the inputs. The
• Functions are: recipe depends on this inputs
• Declared when first defined. It defines and den = value - mu
stores the code récipe
return den/sigma #It returns a value
• Called when needed in action
• Their result typically depends on an input #Up to this point we have just stored normalized, but
• They typically provide one or several never executed it!
outputs as a result of applying the recipe
• Why functions? normalize(3, 1.4, 2) #Now we execute the code recipe
with some specific inputs
• Avoids code repetition → More
maintainable normalize(a,b,c) #I can also call the function using
• Makes the code more readable variables as inputs, or even expressions!
• Allows you to share your code
• Generalizes your code OPT 17
UPV
OTHER DATA Learn more...
STRUCTURES
OPT 18
UPV
TUPLES
Static data structure
OPT 19
UPV
DICTIONARIES
Indexed key-value
• For lists, you access elements by user_ages = { '45883251E': 23, '21885251F': 18, '11895
providing an index position, as they are 231A' : 42 }
an ordered collection
user_ages['11111111B'] = 49
• Dictionaries are not ordered (i.e., there is
print(user_ages['21885251F'])
no numerical position).
print('11895231A' in user_ages)
• The information in a dictionary is
accessed by providing a key, that for key in user_ages.keys():
uniquely identifies the value stored value = user_ages[key]
• They are known as key-value structures, print(key, value)
as you need keys to access and store
values
• When do we use dictionaries?
• Fast access to unordered information
• Less memory efficient
• Counting items
• Defining complex data structures (e.g. OPT 20
JSON) UPV
OPT
UPV
THANK YOU!
anconpei@eio.upv.es
Andrea Conchado Peiró
Víctor Sánchez Anguix vsanchez@eio.upv.es