0% found this document useful (0 votes)
41 views23 pages

02 - CM2015 - Variables, Control Flow and Functions (2022-10)

Class slide notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views23 pages

02 - CM2015 - Variables, Control Flow and Functions (2022-10)

Class slide notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

CM2015 – Programming with Data [SIM – UOL]

Topic 2
Variables, Control Flow and Functions

Learning Outcomes
After completing this topic and the recommended reading, you should be able
to:
• Import modules, and use them to compute basic statistics.
• Use logic and iteration.
• Identify and use correct syntax and explain the purpose of built-in
variable types int, float and list.

Prepared by Koh Chung Haur @ 2022 (version 2022.1) 1


CM2015 – Programming with Data [SIM – UOL]

1. Variables, Data Types and Structures in Python

Variables
• Variable is a named piece of memory whose value can change during the
running of the program; constant is a value which cannot change as the
program runs.
o Python doesn’t use constant
• Every variable in Python is an object.
• Variable can be rebind to another object of different data type.
• Most variables are immutable objects, i.e., new value is created and
rebind to variables, the old value became “garbage”.
• Example:

Variable Name
• We use variable names to represent objects (number, data structures,
functions, etc.) in our program, to make our program more readable.
o All variable names must be one word, spaces are never allowed.
o Can only contain alpha-numeric characters and underscores.
o Must start with a letter or the underscores character.
o Cannot begin with a number.
o Case-sensitive
o Standard way for most things named in Python is lower with under
§ Lower case with separate words joined by an underscore
• No use of reserved words.

Prepared by Koh Chung Haur @ 2022 (version 2022.1) 2


CM2015 – Programming with Data [SIM – UOL]

o if; else; for; def; etc.

Mutability
• Mutable: objects that can be modified.
o Python list and dictionary are mutable objects.
o Example:

• Immutable:
o Python tuple and others are immutable objects.
o Example:

Aliasing
• Assign a variable to another variable
• Object type: A reference/pointer is copied.
o Example:

• “Non-object” type: A duplicate is copied when changes is made.


o R program example:

• Make a copy of the object

Prepared by Koh Chung Haur @ 2022 (version 2022.1) 3


CM2015 – Programming with Data [SIM – UOL]

o Example:

o https://pythontutor.com/live.html#mode=edit

Comments
• Not processed by the computer, valued by other programmers.
• Header comments
o Appear at beginning of a program or a module
o Provide general information
• Step comments or in-line comments
o Appear throughout program
o Explain the purpose of specific portion of code
• Often comments delineated by
o // comment goes here
o /* comment goes here */
o # Python uses this

Python Operations
• Assignment Operator
o “=”
o Example:
§ a = 67890/12345
# compute the ratio, store the result in ram, assign to a
# the value of a is 5.499392
§ b=a
# b pointing to value of a

Prepared by Koh Chung Haur @ 2022 (version 2022.1) 4


CM2015 – Programming with Data [SIM – UOL]

• Output
o “print()”
o Example:
§ print(‘Hello World!’) # print the string literals
§ print(a) # print the value of a

Data Types in Python


• Declaration of variables in Python is not needed
o Use an assignment statement to create a variable

• Float
o Stores real numbers
o a = 4.6
o print(type(a))

• Integer
o Stores integers
o b = 10
o print(type(b))

• Conversion
o int(a) # convert float to int => 4
o float(b) # convert int to float => 10.0

Prepared by Koh Chung Haur @ 2022 (version 2022.1) 5


CM2015 – Programming with Data [SIM – UOL]

• Basic arithmetic operators


o 3+2 # Addition => 5
o 5–2 # Subtraction => 3
o 5 * -2 # Multiplication => -10
o 5 / 2.5 # Division => 2.0
o 2**2 # Exponentiation => 4
o 10 % 3 # Modulus => 1
o 10 // 3 # Floor Division => 3

• String
o Stores strings
o phrase = ‘All models are wrong, but some are useful.’
o phrase[0:3] # slicing character 0 up to 2
=> All
o phrase.find(‘models’) # find the starting index of word
=> 4
o phrase.find(‘right’) # word not found
=> -1
o phrase.lower() # set to lower case
=> ‘all models are wrong, but
some are useful.’
o phrase.upper() # set to upper case
=> ‘ALL MODELS ARE
WRONG, BUT SOME ARE
USEFUL.’
o phrase.split(‘,’) # split strings into list, base on delimiter
=> [‘All models are wrong’,
‘ but some are useful.’]

Prepared by Koh Chung Haur @ 2022 (version 2022.1) 6


CM2015 – Programming with Data [SIM – UOL]

• Boolean
o Stores logical or Boolean values of TRUE or FALSE
o k=1>3
o print(k)

o print(type(k))

• Logical operators
o Conjunction (AND): “and”
o Disjunction (OR): “or”
o Negation (NOT): “not”
a b a and b a or b not a
T T T T F
T F F T F
F T F T T
F F F F T

Data Structures in Python


• Tuples
o Store ordered collection of objects
o Immutable: elements cannot be modified, added or deleted
o Written with round brackets “( )”
§ tuple1 = (“apple”, “banana”, “cherry”, “orange”, “kiwi”,
“melon”, “mango”)
§ tuple2 = (“Handsome Koh”, 4896, 13.14, True)

Prepared by Koh Chung Haur @ 2022 (version 2022.1) 7


CM2015 – Programming with Data [SIM – UOL]

o Accessing elements by indexing


§ tuple1[0] # first element index => ‘apple’
§ tuple1[-1] # last element index => ‘mango’
§ tuple1[2:5] # range of elements => (‘cherry’, ‘orange’,
‘kiwi’)

• Lists
o Store ordered collection of objects; mutable
o Written with square brackets “[ ]”
§ list1 = [“apple”, “banana”, “cherry”]
§ list2 = [“Handsome Koh”, 4896, 13.14, True]
o Changing elements
§ list1.append(“orange”) # add to last position
=> [‘apple’, ‘banana’, ‘cherry’,
‘orange’]
§ list1[2] = “coconut” # modify index element
=> [‘apple’, ‘banana’, ‘coconut’,
‘orange’]
§ list1.remove(“apple”) # delete elements
=> [‘banana’, ‘coconut’, ‘orange’]
§ list1.insert(2, “durian”) # insert element at position
=> [‘banana’, ‘coconut’, ‘durian’,
‘orange’]

• Sets
o Store unordered, unindexed, nonduplicates collection of objects
o Written with square brackets “{ }”
§ set1 = {“apple”, “banana”, “cherry”}
§ set2 = {“apple”, “samsung”}

Prepared by Koh Chung Haur @ 2022 (version 2022.1) 8


CM2015 – Programming with Data [SIM – UOL]

o Set operations
§ set1.union(set2) # Union both sets
=> {‘apple’, ‘banana’, ‘cherry’,
‘samsung’}
§ set1.intersection(set2) # Intersect both sets
=> {‘apple’}

• Dictionaries
o Store unordered collection of objects
o Written with square brackets “{ }”, and “key:value” pair
§ thisdict = {“brand”: “Ford”, “model”: “Mustang”,
“year”: 1964}
o Accessing/modifying elements by key name
§ thisdict[“model”] => ‘Mustang’
§ thisdist[“year”] = 2018 => {‘brand’: ‘Ford’,
thisdist[“color”] = “red” ‘model’: ‘Mustang’,
‘year’: 2018,
‘color’: ‘red’}

Prepared by Koh Chung Haur @ 2022 (version 2022.1) 9


CM2015 – Programming with Data [SIM – UOL]

2. Condition/Decision/Selection/Branching in Python

• A selection (or decision) structure allows a program to perform specific


actions only under certain conditions.
o Evaluate one or more alternatives.
o Selectively execute statements.
o Requires a condition (to test if it is true or false) to determine
when to execute statements, or to decide on the path to be taken.
• Single alternative
o A single block of statements to be executed or skipped.
o Example: if

• Dual alternative
o Two blocks of statements, one of which is to be executed, while
the other one is to be skipped.
o Example: if…then…else

Prepared by Koh Chung Haur @ 2022 (version 2022.1) 10


CM2015 – Programming with Data [SIM – UOL]

• Multiple alternative
o More than two blocks of statements, only one of which is to be
executed and the rest skipped.
o Example: if…then…else if…else; switch case

Control Flow Structures in Python


• No parentheses are needed to enclose the control flow structures
• : is needed after the if, elif, else, for, while statements
• Indentation is required for the block after the control flow statement

Conditional Statements
• Single alternative

Prepared by Koh Chung Haur @ 2022 (version 2022.1) 11


CM2015 – Programming with Data [SIM – UOL]

o Python syntax:

o Example:

• Dual alternative
o Python syntax:

o Example:

• Multiple alternative (nested if)


o Python syntax:

Prepared by Koh Chung Haur @ 2022 (version 2022.1) 12


CM2015 – Programming with Data [SIM – UOL]

o Example:

Prepared by Koh Chung Haur @ 2022 (version 2022.1) 13


CM2015 – Programming with Data [SIM – UOL]

3. Iteration/Repetition/Loop in Python

• A repetition (or loop) structure causes a statement or set of statements to


execute repeatedly, under some conditions.
o Repeat statements more than once.
o Needs a stop condition, i.e., the program will continue to loop until
some condition is met.
à Be careful not to create infinite loops
• Allow a programmer to avoid duplicate code.
o Duplicate code makes a program large.
o Write a long sequence of statements is time consuming.
o If part of the duplicate code must be corrected or change, then the
changes must be done many times.
• Condition-controlled loops
o Test condition before/after performing an iteration.
o Condition tested for true or false value.
o Example: while loop; repeat loop
• Count-controlled loops
o Iterates a specific number of times, moving through all the
elements of data structure.
o Example: for loop
• Nested loops
o All loops can be nested, that is, a loop contained inside of another
loop.
o Inner loop goes through all its iterations for each iteration of outer
loop.
o Inner loops complete their iterations faster than outer loops.
o Often used to step through two-dimensional arrays.

Prepared by Koh Chung Haur @ 2022 (version 2022.1) 14


CM2015 – Programming with Data [SIM – UOL]

Iteration Structures in Python


• Count-controlled Loop
o Python syntax:

o Example:

• Terminating early
o continue: stops the current iteration and moves to next iteration.
o break: stops the loop entirely.
eval( ) - changes the type based on the input

Prepared by Koh Chung Haur @ 2022 (version 2022.1) 15


CM2015 – Programming with Data [SIM – UOL]

o Example:

• Condition-controlled Loop
o Python syntax:

o Example:

Prepared by Koh Chung Haur @ 2022 (version 2022.1) 16


CM2015 – Programming with Data [SIM – UOL]

4. Functions in Python
create pipeline for the data cleaning and machine learning processes. ** more intelligent code

Writing a user-defined Functions


• Arguments are the inputs to the function module.
• Return statement are the output from the function module.
• Function Definition
o Python syntax: Header comment for the function. no need to write docstring in the midterm

o Example:

Scope of Function
• Global scope
o Variables from global scope is available everywhere.
o Variables created outside of a function can be used by any
functions.

Prepared by Koh Chung Haur @ 2022 (version 2022.1) 17


CM2015 – Programming with Data [SIM – UOL]

o Example:

• Local scope
o Variables from local scope is not available elsewhere.
o Variables created inside of a function cannot be used outside of the
function.
o Example:

• LEGB Rule
o Local à Enclosing à Global à Built-in scope
o Example:

o https://pythontutor.com/live.html#mode=edit

Functions with Mutable Object


• Pass as reference
o A pointer to the object is passed to the arguments of function.
o Changes made in function modified the original object.

Prepared by Koh Chung Haur @ 2022 (version 2022.1) 18


CM2015 – Programming with Data [SIM – UOL]

o Example:

• Make a copy
o A new copy of the reference object is made, changes will not
modify the original object.
o Example:

o https://pythontutor.com/live.html#mode=edit

Libraries
• SciPy
o Python library used for scientific computing
o Contains modules for optimization, linear algebra, integration,
interpolation, etc
• NumPy
o Python library used for working with arrays
o Performs numerical processing
• Pandas
o Python library used for data manipulation and analysis
o Work with heterogenous data, as data frame

Prepared by Koh Chung Haur @ 2022 (version 2022.1) 19


CM2015 – Programming with Data [SIM – UOL]

Prepared by Koh Chung Haur @ 2022 (version 2022.1) 20


CM2015 – Programming with Data [SIM – UOL]

5. Exercises

2.205 Programming Exercise


• Write a piece of code that can compute the highest of two values as a
Jupyter Notebook.

2.403 Functions and Libraries


• Refers to “2.403 Topic 2 – lab 1.html”

2.405 Date and Time Data


• Refers to “2.405 Topic 2 – lab 2 datetime.html”

Prepared by Koh Chung Haur @ 2022 (version 2022.1) 21


CM2015 – Programming with Data [SIM – UOL]

6. Practice Quiz
• Work on Practice Quiz 02 posted on Canvas.

Prepared by Koh Chung Haur @ 2022 (version 2022.1) 22


CM2015 – Programming with Data [SIM – UOL]

Useful Resources

o http://

Prepared by Koh Chung Haur @ 2022 (version 2022.1) 23

You might also like