0% found this document useful (0 votes)
27 views

Slides 02 Python

This document is a summary of key topics from Michael Burrell's course on Python expressions, types, and variables. It covers Python basics like its creation in 1994 and its multi-paradigm nature. It also discusses values and types in Python like integers and floats. Finally, it examines variables, how they can be assigned and reassigned values, and how to increment and decrement them. The overall document provides an introduction to fundamental Python concepts.

Uploaded by

김은총
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Slides 02 Python

This document is a summary of key topics from Michael Burrell's course on Python expressions, types, and variables. It covers Python basics like its creation in 1994 and its multi-paradigm nature. It also discusses values and types in Python like integers and floats. Finally, it examines variables, how they can be assigned and reassigned values, and how to increment and decrement them. The overall document provides an introduction to fundamental Python concepts.

Uploaded by

김은총
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Introduction

Arithmetic Readings
Variables Python
Conclusion

Expressions, types, variables


Python basics

Michael Burrell

August 25, 2023

Michael Burrell Expressions, types, variables


Introduction
Arithmetic Readings
Variables Python
Conclusion

Readings

Chapter 1 — 1.5, 1.6, 1.7


Chapter 2 — 2.2, 2.3, 2.4
Chapter 3 — 3.1

Michael Burrell Expressions, types, variables


Introduction
Arithmetic Readings
Variables Python
Conclusion

Python

Python is a programming language created by Guido von


Rossum (originally just in his spare time)

It was first released in 1994

Python is relatively simple for beginners and is


multi-paradigm (mixes in a variety of programming
disciplines)

Michael Burrell Expressions, types, variables


Introduction
Arithmetic
Types
Variables
Conclusion

Values and types

4 + (2 ** 2 + 7) / 2
Expressions in Python follow the usual algebraic order of
operations

Python will evaluate the entire expression, one operation


at a time

Michael Burrell Expressions, types, variables


Introduction
Arithmetic
Types
Variables
Conclusion

Values and types

4 + (2 ** 2 + 7) / 2
Expressions in Python follow the usual algebraic order of
operations

Python will evaluate the entire expression, one operation


at a time

We have to be conscious of types. . .

Michael Burrell Expressions, types, variables


Introduction
Arithmetic
Types
Variables
Conclusion

Values and types

English name Python name Example values


Integer int

Michael Burrell Expressions, types, variables


Introduction
Arithmetic
Types
Variables
Conclusion

Values and types

English name Python name Example values


Integer int 0, 5, -17, 62, -3, -1
Real float

Michael Burrell Expressions, types, variables


Introduction
Arithmetic
Types
Variables
Conclusion

Values and types

English name Python name Example values


Integer int 0, 5, -17, 62, -3, -1
Real float 0.0, -1.2, 34.7728, -6.0

Every value in Python has a type

At first we’ll only look at two types

Michael Burrell Expressions, types, variables


Introduction
Arithmetic
Types
Variables
Conclusion

Values and types

English name Python name Example values


Integer int 0, 5, -17, 62, -3, -1
Real float 0.0, -1.2, 34.7728, -6.0

Every value in Python has a type

At first we’ll only look at two types

We can use the built-in type function to see the type of


an expression

Michael Burrell Expressions, types, variables


Introduction
Arithmetic
Types
Variables
Conclusion

int vs float

int and float values are both used to represent


numbers, but are used for different purposes

ints cannot store non-integer values (duh)

floats often cannot store values exactly, however (try


0.7 + 0.1)

Michael Burrell Expressions, types, variables


Introduction
Arithmetic
Types
Variables
Conclusion

int vs float

int and float values are both used to represent


numbers, but are used for different purposes

ints cannot store non-integer values (duh)

floats often cannot store values exactly, however (try


0.7 + 0.1)

Generally, we’ll use ints by default (i.e., use ints unless


we know we might need non-integer values)

Michael Burrell Expressions, types, variables


Introduction
Arithmetic
Types
Variables
Conclusion

Type conversions

Values can get converted from one type to another

Soon we’ll look at doing this ourselves, but first we’ll look
at implicit type conversions (type promotions)

Sometimes ints get turned into floats automatically


(try type(4 / 2))

Michael Burrell Expressions, types, variables


Introduction
Arithmetic
Types
Variables
Conclusion

Type conversions

With +, -, *, ** and %, the type of the expression is the


same as the types of the operands
E.g., type(3 * 4) is int; e.g., type(3.5 * 2.0) is
float

If there is a mismatch, one operand gets promoted from


int to float (e.g., type(3 * 2.5) is float)

/ (division) is a special case: it’s always a float1 , even if


both operands are ints
E.g., 5 / 2 is 2.5, a float

1
This is only true in Python 3, not Python 1 or Python 2
Michael Burrell Expressions, types, variables
Introduction
Arithmetic
Types
Variables
Conclusion

Type conversions

We can also convert values from one type to another


explicitly

There are two functions at our disposal: int and float

E.g., int(3.2) converts 3.2 into the integer value 3

E.g., float(4) converts 4 into the real value 4.0

Michael Burrell Expressions, types, variables


Introduction
Variables
Arithmetic
Strings
Variables
Booleans
Conclusion

Variables

Using Python as a calculator is fun and all, but generally


we’re interested in writing programs

These programs should respond to user input

From now on, everything we do is going to involve


variables

Like in algebra, variables are going to be names (symbols)


which have a value bound to them

Michael Burrell Expressions, types, variables


Introduction
Variables
Arithmetic
Strings
Variables
Booleans
Conclusion

Variables

x=3
To introduce a new variable, you name it and put it on
the left of the = sign

You can use any expression you want to assign to a


variable

Once a variable is introduced, you can use it in any


expression

Michael Burrell Expressions, types, variables


Introduction
Variables
Arithmetic
Strings
Variables
Booleans
Conclusion

Variables

Unlike in algebra, variables can be (and very commonly


are) changed

Reassigning to a variable looks just like introducing a new


variable

x = 3
y = x * 2
x = x - 1
print(x + y) – what’s the answer here?

Michael Burrell Expressions, types, variables


Introduction
Variables
Arithmetic
Strings
Variables
Booleans
Conclusion

Incrementing and decrementing

Adding a value onto a variable is called incrementing

Subtracting a value from a variable is called decrementing

Python gives a shorter syntax for incrementing and


decrementing
Long syntax Short syntax
x = x + 2 x += 2
x = x - 1 x -= 1
x = x * x x *= x

Michael Burrell Expressions, types, variables


Introduction
Variables
Arithmetic
Strings
Variables
Booleans
Conclusion

Strings
So far we know of 2 types in Python

Michael Burrell Expressions, types, variables


Introduction
Variables
Arithmetic
Strings
Variables
Booleans
Conclusion

Strings
So far we know of 2 types in Python

The next type which we’re going to learn is a string


(called str in Python)

It represents any sort of text (of any length)

String values must be enclosed in either "double


quotes" or ’single quotes’

E.g., x = ’hello there!’

Michael Burrell Expressions, types, variables


Introduction
Variables
Arithmetic
Strings
Variables
Booleans
Conclusion

Operations on strings

There is a little bit of arithmetic we can perform on strings:


1 Append/concatenate with another string with the +
operator. E.g., ’hello’ + ’ ’ + ’friend!’
However, all the operands must be strings

If we want to append a value that’s not a string, we


must first convert it to a string with the str function
2 Multiply by an integer. E.g., ’abc’ * 3 will give us
’abcabcabc’

Michael Burrell Expressions, types, variables


Introduction
Variables
Arithmetic
Strings
Variables
Booleans
Conclusion

Booleans
The last type we’ll look at today is the booleans (bool in
Python)

It can only have two possible values: True or False

Comparison expressions result in booleans, e.g., using


operators like <, >, == (NB: use 2 equal signs to test for
equality), != (not equal), <=, >=

If booleans are converting to integers (e.g., (3 < 4) *


2), True is interpreted as 1 and False is interpreted as 0

Michael Burrell Expressions, types, variables


Introduction
Variables
Arithmetic
Strings
Variables
Booleans
Conclusion

Values and types

English name Python name Example values


Integer int 0, 5, -17, 62, -3, -1
Real float 0.0, -1.2, 34.7728, -6.0
Text str ‘’, ‘hello’, “goodbye”
Truth bool True, False

Michael Burrell Expressions, types, variables


Introduction
Arithmetic
Variables
Conclusion

Conclusion

Python has a number of arithmetic operations, similar to


in math

Each Python value has a type (e.g., int, float, str,


bool)

We can assign values to variables and change the values


in those variables

Michael Burrell Expressions, types, variables

You might also like