Docsity

Python Functions and Variable Scope, Study notes of Anatomy

Partial preview of the text

Download Python Functions and Variable Scope and more Study notes Anatomy in PDF only on Docsity!

Python Functions

CS106AP Lecture 6

Roadmap

Programming Basics^ The Console Images Data structures Midterm Graphics Object-Oriented Programming Everyday Python

Life after CS106AP!

Day 1!

Today’s questions How do we translate what we know from Karel into regular Python code? How can we make our code more flexible by producing different outputs depending on the input?

Today’s topics

  1. Introduction and Review
  2. Range For Loops
  3. Python Functions
  4. Variable Scope
  5. What’s next?

Sonja Johnson-Yu

Sonja Johnson-Yu

Sonja Johnson-Yu

Review

What is a variable?

A variable is a container for storing a data value. num_flowers = 5 num_flowers 5 variable’s name

What is a variable?

A variable is a container for storing a data value. num_flowers = 5 num_flowers 5 variable’s value

Expressions

Recall: expressions

● The computer evaluates expressions to a single value ● We use operators to combine literals and/or variables into expressions

Arithmetic operators

  • Multiplication / Division // Integer division % Modulus (remainder)
  • Addition
  • Subtraction Operator Precedence () 1 *, /, //, % 2 +, - 3 Integer division takes the largest integer that is equal to or smaller than the quotient

Integer Division Practice!

● 5 + 1 // 2 ● 9 // 3 ● 8 // 3 ● -8 // 3 Integer division takes the largest integer that is equal to or smaller than the quotient