Byte-Sized Python - part 2




Last week, we showed that variables have names, and they store values. They are also classified as different types: int and float types are numeric types, str type is a string type, bool type is a Boolean type, and a null type called 'None.' The more complex types are called classes, which we will deal with later. Once defined, variables can be referenced, combined, and manipulated in multiple ways. You use operators to manipulate variables, keeping in mind some commonsensical rules.


​​•Addition +

•Subtraction -

•Division /

•Multiplication *

•Power **

•Modulus %

•Floor division //


These operators are very similar to math operators. You can add two int variables or float variables or a combination of them easily.


my_burger = 1.10

meat_less_discount = 0.40


meat_less_burger = my_burger - meat_less_discount


You can also have conditional checks using an 'if' statement


# Always check for nonsensical conditions

if (meat_less_burger < 0):

print('**Error***')


Similarly, you can add two strings (technically called concatenation). However, you cannot add a string and a number together.


my_customer = 'Sam'


print( 'Thank you for shopping ' + my_customer )


There are also ‘reserved keywords’ that Python programming language uses that you cannot use for your variable names. For example - ‘and’, ‘break’, ‘if’, ‘finally’, ‘del’, ‘pass’, and ‘raise’, are all reserved words used by the language and therefore cannot be used as variable names.


Do you know the importance of manipulating variables and validating them correctly? Check out this video and figure out what went wrong with the vending machine where a customer gets 10 meatless burgers and 1 hamburger for free.


https://www.youtube.com/watch?v=1oFpTNsPu_w


3 views0 comments