You are on page 1of 27

Variables in Python

Just like Scratch!

Kelechi
We are about making Bold Statements
in Bold Prints.
-

Quote from Kelechi

Zuvaa.com uses variables!


items_in_cart = 0

items_in_cart = 1

Why use variables?


How do I
make this
easier?

Why use variables?

I use variables so I
dont have to write
each one by hand

How Do We Use Variables?

Must start with letter or underscore


Good variable names:

Bad variable names:

product

3products

title

$price

number

@thisplace

_price

!discount

Can have letters, numbers, and underscores


Good Variable Names:

Bad Variable Names:

product_3

product-3

x4

price$

a_long_variable_name

product title

VariaBle NaMes aRe cAse-seNsitiVe


product DOES NOT EQUAL Product
product DOES NOT EQUAL pRoduct
product DEFINITELY DOES NOT EQUAL PRODUCT

Programming Style

lowercase_with_words_separated_by_underscores
OK Variable Names:

Stylish Variable Names:

producttitle

product_title

Title

title

itemPrice

item_price

aMount

amount

Variable Values

I use
variables by
setting them
to a value

= does not mean equal


product_price = 39.99
= does mean set to

You can set variables to all kinds of values!


Strings:

product_name = Ren White Jumpsuit

Numbers:

items_in_cart = 3

Booleans:

paid = True

Other Variables: total_items = items_in_cart

Variables are fickle


Variables only remember the most recent thing they have been set to.

items_in_cart = 0
items_in_cart = 1
items_in_cart will only show 1

Variables can even be set to themselves!

total_items = total_items + 1
Why does this work?

Right to Left

total_items = total_items + 1

Lets do a little practice


product_title = African Print Cape
product_price = 129.00

What will the computer say when we type in


product_title?
What will it say when we type in product_price?

More practice!
product_price = 129.00
product_price = 30.00

What will the computer say when we type in


product_price?

More practice!
product_price = 129.00
product_price = product_price - 70

What will the computer say when we type in product_price?

Bonus Tip: Shortcut

product_price = product_price - 70
product_price -= 70

Extra Bonus Tip: input function

Setting input to a variable

Lets Use Some Variables!

You might also like