#!/usr/bin/python3 # -*- coding: utf-8 -*- """ Variables """ # There are no declarations in python3, only assignments. # Use lower_case_with_underscores for variable names. some_var = 5 some_var # => 5 # Accessing a previously unassigned variable names is an error / exception. # >>> print(some_unknown_var) # If we ran the line above, it raises a NameError, learn to read errors!!!! # NameError: name 'some_unknown_var' is not defined my_string = "This is a string" my_string = 'This is a string too with an " in it' my_string2: str = ( "The : str part is optional, and for now is just a helpful note, called a type hint" ) my_string3: str = "The thing below isn't a number, but a string" my_string4: str = "4" my_int: int = 4 # This is a real int my_string = "We're re-using the above name (my_string) for a new object. Variables (labels) can be re-assigned to new objects" # They can also be assigned to new objects of different type, but that's usualyl not good style. my_string = 4 # Variables are just references to objects stored in memory # These references are like numeric addresses # The variable name does not have a type, but the object being referenced does. myint: int = 7 print(type(myint)) print(id(myint)) print(myint) myfloat: float = 7.0 print(type(myfloat)) print(id(myfloat)) print(myfloat) myfloat = float(myint) print(type(myfloat)) print(id(myfloat)) print(myfloat) mystring = "hello" print(type(mystring)) print(id(mystring)) print(mystring)