0% found this document useful (0 votes)
3 views5 pages

00 Intro

The document serves as an introduction to Python programming, covering basic operators, variable assignment, naming conventions, and functions. It explains the differences between mathematical equality and Python assignment, as well as the structure of function definitions and calls. Additionally, it highlights the distinction between expressions and statements in Python.

Uploaded by

Sohail Khan
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)
3 views5 pages

00 Intro

The document serves as an introduction to Python programming, covering basic operators, variable assignment, naming conventions, and functions. It explains the differences between mathematical equality and Python assignment, as well as the structure of function definitions and calls. Additionally, it highlights the distinction between expressions and statements in Python.

Uploaded by

Sohail Khan
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/ 5

CSCA20 Worksheet 0 – Introduction

1 Python as a calculator
Let’s start with some basic operators:

Symbol Operation Expression English description Value


+ addition 11 + 56 11 plus 56 67
- subtraction 12 - 2
* multiplication 4*5
** exponentiation 2 ** 5
/ division 8/3
% remainder 8%3

Q. With 8 % 3 we get the remainder from “long division”, but 8 / 3 doesn’t give us the long-
division quotient. How do we get the quotient?

A.

Expressions with multiple operators:

EXERCISE: List the basic operators in order of precedence.

Q. What happens when we try to evaluate an expression that can’t work (e.g., 4 / 0)?

A.

2 Python types
Let’s use Python’s function type to find out the types of the numbers we’ve been working with:

There are also functions that take a value of one type and “convert” it to another:

1
3 Variables
A variable in Python is a name of some value.

3.1 Variable assignment


Form of an assignment statement:
variable = expression

How it’s executed


1. Evaluate the expression on the right-hand side.
2. Associate the result with the variable on the left-hand side.
Examples:

About Variable Assignment

• Python variables look like math variables.

• This could be Python or math:

p = 5
q = p * 7

• But they are very different!

Equality vs assignment:

• In math, p = q + 10 states a fact about the value of p and of q + 10: that they are equal.

• In Python, p = q + 10 means something completely different, namely assignment:

– get the value that corresponds to variable q


– add 10 to that value
– assign variable p the resulting value: the variable p is now a name for the resulting value
/ it refers to the resulting value

• This is why you might reasonably say x = x + 1

• We say “x is assigned x + 1” or “x gets x + 1”

• Programming languages usually have different symbols for assignment and equality. In
Python, the symbol for equality is ==

2
• In math, these are inconsistent:
p = 5
q = 7
p = q + 10

– p can’t be both 5 and 17!


– But in Python, it makes perfect sense. p starts out referring to 5, but then changes to
refer to 17.

• You can change a variable’s value as many times as you want. You can change it to a value
of a different type, too.
In mathematics, equations are descriptions that are simultaneously true. In Python, assign-
ment statements look like equations but really specify a sequence of steps.
• Assignment is not symmetric
math Python
sum = a + b legal
same meaning
a + b = sum illegal
Observation:
• What does this do?
– x = 37
– y = x + 2 # y is now 39
– x = 20 # Is y now 22?
• That’s not how assignment works!

3.2 Naming variables


• These names are all perfectly all right in Python:
x
my_average
fraggle
• There are a few rules about variable names:
– Must start with a letter (or underscore)
– Can include letters, digits, and underscores, but nothing else
– Case matters:
age = 11
aGe # Error! This is not defined.
• As well as rules, there are conventions about choosing names well:
– We choose meaningful names for the sake of the humans who will read our code. For
example, if you are adding something up, “sum” is better than “x”.
– For names that include multiple words, we use “pothole case”: e.g., average_grade
– We’ll get lots of practice choosing good names over the term.

3
4 Functions
4.1 Motivation
Q. Consider a cookbook with 12 cake recipes, 3 of them involving a butter-cream frosting. We
could repeat that part of the recipe 3 times. What is a better option?

A.

4.2 Defining a function


Give an example of defining a function in math:

Example function defn:


Form of a Python function definition:

def function name(parameters):


body

def: a Python keyword


parameters: 0 or more parameters, comma separated
body: 1 or more statements

4.3 Calling a function


So far, we have defined what f is, but we haven’t used it. When we call a function, we ask Python
to execute it (carry it out).

Form of a function call: Example function calls:

function name(arguments)

How it’s executed:


1. Each argument is an expression. Evaluate these expressions,
in order. (The value of each expression is a memory address.)
2. Inside the function, the corresponding parameters will refer to
these memory addresses.
3. Execute the body of the function.

4
4.4 Nested function calls

5 Expressions vs. Statements


Example Python expressions:

Example Python statements:

The distinction in Python is complex. In this course, you can think of it as follows:

• expressions can be reduced to a value

• statements are commands to do something

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy