Enhance your understanding of Python programming with these detailed ENGG1810 USYD course notes. This document is designed to support students in grasping the fundamentals of Python and advancing through more complex topics effectively. Ideal for students and professionals, these notes cover essent...
● Definition: Dictionaries store key-value pairs and are mutable and ordered (as of python
3.7).
● Examples:
○ Creating and accessing dictionaries:
computer = {'brand': 'Apple', 'model': 'iMac', 'year': 2007}
print(computer['model']) # Output: iMac
○ Modifying dictionaries:
computer['year'] = 2010 # Updates the year
computer['color'] = 'silver' # Adds a new key-value pair
Basics of Control Flow
● Control flow in programming refers to the direction in which the program executes. In
Python, this is primarily managed through conditional statements and loops. This lecture
focuses on conditional statements which allow the program to react differently depending
on the input or other data.
Conditional Statements
Conditional statements are the backbone of decision making in Python. They execute a certain
block of code based on a condition. The primary statements used are:
1. if Statement:
○ The if statement is used to test a condition and execute a block of code if the
condition is true.
○ Syntax:
if condition:
# execute this block
○ Example:
if weather == 'sunny':
print("Let's go outside!")
2. else Statement:
○ The else statement follows an if statement and is executed if the if statement’s
condition is false.
○ Syntax:
if condition:
# execute this block
else:
# execute this block
○ Example:
, if weather == 'sunny':
print("Let's go outside!")
else:
print("We might need an umbrella.")
3. elif (else if) Statement:
○ The elif statement is used to check multiple expressions for TRUE and execute a
block of code as soon as one of the conditions evaluates to TRUE.
○ Unlike else, there can be multiple elif blocks following an if.
○ Syntax:
if condition1:
# execute block 1
elif condition2:
# execute block 2
else:
# execute block 3
○ Example:
if weather == 'sunny':
print("Let's go outside!")
elif weather == 'rainy':
print("We need an umbrella.")
else:
print("It's a pleasant day.")
, Lecture 3
While Loops
● A while loop in Python repeatedly executes a target statement as long as a given
condition is true. Here’s how you structure a while loop:
○ while <condition>:
# Execute these actions
● Example: Counting Race Finishers Imagine a scenario where athletes are finishing a
race, and we want to print their finish positions as they cross the line:
○ person = 0
while person < 100:
person += 1
print(person)
● This code will print numbers 1 through 100, simulating athletes finishing from 1st to
100th place.
For Loops
● A for loop is used for iterating over a collection (like a list, tuple, set, or dictionary) or an
iterator. The basic syntax of a for loop is:
● for variable in collection:
# Execute actions
● Example: Iterating Over a List of Students
● students = ['Caren Han', 'Henry Weld', 'Annie Sun', 'Lilian Hunt']
for student in students:
print(student)
● This will print each student's name from the list.
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller harryrickard. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $15.49. You're not tied to anything after your purchase.