Programming in Python

BEES 2021

Agenda

  • Variables
  • Expressions
  • Statements
  • Operators
  • Abbreviated Assignment
  • Logical Operators
  • Order of Operations

Variables

  • A variable is a named memory location. Think of a variable as a box.
  • It contains a value. Think of the value as the content of the box.
r = 10
p = 3.14 * 2 * r

10

62.8

Variable Example

Variable names

  • A variable name (identifier) can be any one word
     that:
    • Consists of letters, numbers, or _
    • Does not start with a number
    • Is not a Python reserved word (keyword)

 

 

 

  • Python is case-sensitive
    • "User" is not the same as "user"

Variable names example

  • Numeric

 

 

  • Boolean

 

  • String
  • int (25, -32, 0, 1024)
  • float (1.5, 0.005, -80.12)
  • complex (1+3j, 12j, -2-5j)

 

  • True, False

 

  • "", "Python", "B"

 

Data types

Expression

  • An expression is a combination of values, variables, operators, and call to functions
  • Expressions need to be evaluated and result in a value
# Now we have basic types and variables, we can do stuff with them
x = 1
y = 2
x + y - 2 # This is an expression, the intepretter 
# has to "evaluate it" to figure out the value by 
# combining variables (x, y), values (2) and operators (+, -)

# When we're calling a function (here the "type" function), 
# we're also evaluating an expression
type(3.14)

statement

  • A statement is everything that can make up a line of Python code
  • A statement does something!
# Statements are instructions to the intepreter 

x = 1 # This is a statement, your telling Python, make x refer to the value
# 1

if x > 0: # Conditionals (like if (and others we'll see soon, 
  # are also "statements"))
  print("boo")

Arithmetic Operators

Symbol Meaning Precedence
+ Addition Low
- Subtraction Low
* Multiplication Medium
/ Division Medium
// Floor Division Medium
% Remainder (mod) Medium
** Exponentiation High

Operator Example

Order of operations

operator overloading

5 + 10 + 12 # We can use "+" to add together strings of numbers
# Some arithmetic operators are also "overloaded" to work with strings

"This" + "is" + "contrived" # The '+' concatenates strings

# Note, this doesn't work because Python doesn't know how to add a string and
# a number together

"this" + 5

# How to fix this?

Abbreviated Assignment

Type conversion

  • To integer

 

  • To float

 

  • To string

 

  • To boolean
x = int("25") 
x = int(34.287)
x = int(True)
x = float("34.287") 
x = float(12)
x = float(False)
x = str(34.287)
x = str(12)
x = str(True)
x = bool("text") 
x = bool(0)
x = bool(34.287)

Type conversion example

# Remember everything in Python has a type, discoverable with "type()"

# What's the type of x?
x = 12
y = 5
type(x)
type(y)

# So what's the type of our expression result?
type(x // y)

type(x / y) # And this one ?

type(x + 5.0)


Logical operators

0
 Advanced issues found
 
  • Booleans are used for making decisions in your program
  • To do this we use logical operators
  • Some logical operators:

 

>

>=

<

!=

==

and

or

<=

Logical Operators example

putting it all together

Lecture 2 challenge

Questions?

BEES - Lecture 2

By Narges Norouzi

BEES - Lecture 2

  • 348