Week 1 2 Python Fundamentals
Week 1 2 Python Fundamentals
Code Cell
Set Your Script Name
Python fundamentals
Outline
▪ Python Functions
▪ Control Flows
Variables and Types
▪ Variable
– Specific, case-sensitive name
– Call up value through variable name
▪ Create a string
– A string is a sequence of characters.
– The characters are specified either between single (‘)
or double (“) quotes
– E.g., a = “abc” or a = ‘abc’
▪ Concatenate strings
– ‘+’ operator
– join
String Operations in Python
▪ Repeat a string
▪ Tokenize a string
▪ Replace
Data Structures In Python
▪ Lists
▪ Tuples
▪ Dictionaries
▪ Sets
Lists
▪ Sorting sequences
– sort: it modifies the original list
– sorted: it returns a new sorted list and
leaves the original list unchanged.
– The parameter reverse=True (both to
sort and sorted) can be given to get
descending order of elements
Common Operations on List
▪ Concatenate lists
– +: return a new list. The original list is unchanged
– extend: modify a list in place
– append: add a single element to the end of a list
➢ If append another list onto a list, it will treat another list
as a single object
Tuples
return statement
return value
Python Functions
▪ Note that the named arguments didn’t need to be in the same order as in the
function definition. The named arguments must come after the positional
arguments.
Control Flows in Python
▪ With control flow, you can execute certain code blocks conditionally and/or
repeatedly: these basic building blocks can be combined to create
sophisticated programs.
▪ At each iteration, the variable i refers to another value from the list in order.
Decision making with the if statement
▪ Sometimes, you will find that you will need to handle for exceptions.
▪ When continue executes, the current iteration of the loop terminates, and
the execution continues with next iteration of the loop.
▪ Stopping current iteration and continuing to the next one (do not break the
loop).
Try-except
▪ Unhandled exceptions will cause your program to crash. You can handle
them using try and except statement
▪ If the code in the try block works, the except block is skipped.
▪ If the code in the try block fails, it jumps to the except section.