#!/usr/bin/python3 # -*- coding: utf-8 -*- """ Assignment operator = Assigns values from right side operands to left side operand c = a + b assigns value of a + b into c """ one: int = 1 two: int = 2 three: int = one + two thethree: int = three threemore: int = 3 print(three, thethree, threemore) id(three) id(thethree) id(threemore) # It's the number with an id! # We can delete a variable, which removes the reference, and maybe the object. # If the object had multiple lables pointing to it, then it may not be deleted. # More to come on that later. three == thethree three is thethree three == threemore three is threemore del three del one del two print(thethree) # With floats one_float: float = 1.0 two_float: float = 2.0 three_float: float = one_float + two_float thethree_float: float = three_float threemore_float: float = 3.0 print(three_float, thethree_float, threemore_float) id(three_float) id(thethree_float) id(threemore_float) # It's the number with an id! # Which is which? three_float == thethree_float three_float is thethree_float three_float == threemore_float three_float is threemore_float del three_float # This still exists, because it was just the first alias that was deleted: print(thethree) # The object is garbage collected when the last alias disappears # String concatenation using variables using + hello = "hello" world = "world" helloworld = hello + " " + world print(helloworld) # Mixing operator + between numbers and strings is not supported, but * is ... lotsofhellos = "hello" * 10 print(lotsofhellos) # In variable form: counter = 100 # An integer assignment name = "John" # A string print(name * counter) # To get a number from the user, you need to cast into an int: mynum_bad = input("input a number please") print(type(mynum_bad)) mynum_good = int(input("input a number please")) print(type(mynum_good)) # Complex operations with variables number = 1 + 2 * 3 / 4.0 print(number) # Mod with variables remainder = 11 % 3 print(remainder) # Oh the power of python! squared = 7 ** 2 cubed = 2 ** 3 # %% Assignment variations # Multiple assignment can be useful sometimes a, b = 3, 4 print(a, b) # swapping is a common operation a, b = b, a print(a, b) # These lists of relatively assigned variables can be longer too: a, b, c = 3, 4, 5 print(a, b, c) # heterogenous OK too d, e, f = 1, 2, "john" # Chained assignment is read right to left, 1 into c, into b, into a. a = b = c = 1 # Computer scientists are lazy (a.k.a. efficient): Faster assignment operators """ += Add AND It adds right operand to the left operand and assign the result to left operand c += a is equivalent to c = c + a """ x = 3 x += 2 # x = x + 2 print(x) """ -= Subtract AND It subtracts right operand from the left operand and assign the result to left operand c -= a is equivalent to c = c - a """ x = 3 x -= 2 print(x) """ *= Multiply AND It multiplies right operand with the left operand and assign the result to left operand c *= a is equivalent to c = c * a """ x = 3 x *= 2 print(x) """ /= Divide AND It divides left operand with the right operand and assign the result to left operand c /= a is equivalent to c = c / ac /= a is equivalent to c = c / a """ xval = 3.0 xval /= 2 print(xval) """ %= Modulus AND It takes modulus using two operands and assign the result to left operand c %= a is equivalent to c = c % a """ x = 3 x %= 2 print(x) """ **= Exponent AND Performs exponential (power) calculation on operators and assign value to the left operand c **= a is equivalent to c = c ** a """ x = 3 x **= 2 print(x) """ //= Floor Division It performs floor division on operators and assign value to the left operand c //= a is equivalent to c = c // a """ x = 3 x //= 2 print(x)