Introduction to Python Data Types and Variables ICS 3U0
Today’s Agenda Variables Data Types Declaring and Assigning Variables Strings, Integers and Floating Numbers
Storage and Variables In order to interact with the outside world, programs have to accept Input. Before doing this, however, we need to discuss storage and variables. If the computer asks the user for their name, age, grade, address, or any other information, it should remember that information. Information is stored in memory. A variable is a specific location in memory. It is called a “variable” because its value can be changed (varies).
Declaring Variables When we declare a variable: a space is reserved in memory for that data a name is reserved to identify that data the type of data stored in the variable is specified (for example, is this variable going to be a string or integer) Once a name is reserved, you cannot declare another variable by the same name.
Three Basic Data Types int – an integer value is a positive or negative whole number (..., -3, -2, -1, 0, 1, 2, 3, …) float – a real number involves decimals, such as 0.5, 0.33, 10.7. You can also represent integers as reals, but try to avoid this (-3.0, 4.0) str – a string value is a collection of characters, such as a name, address, or other combinations of letters and numbers
How to Declare Variables in Python To create (or declare) a variable, assign it a value. >>> number = 42 The equals sign, =, is called the assignment operator, since it is used to assign a value to a variable. We can now refer to the variable by its name, anytime we want. For example, to check its current value, type the variable’s name. >>> number 42
Example - Integers a = 8 b = 6-7 print(a, b) What does the type function do? a = 8 b = 6-7 print(a, b) print("The value of a is:", a) print("The value of b is:", b) print(type(a)) print(type(b))
Example - Floating Numbers print(a, b) print("The value of a is:", a) print("The value of b is:", b) print(type(a)) print(type(b))
Example - Strings a = "Mr. Cookit" b = "LOVES programming" print(a, b) print("The value of a is:", a) print("The value of b is:", b) print(type(a)) print(type(b))
Naming Variables – The RULES Variables should start with a lower case letter. Variables can start with an upper case letter or an underscore, but those are special cases and should not be done on a normal basis. After the first lower case letter, the variable may include uppercase and lowercase letters, along with numbers and underscores. Variables may not include spaces.
Naming Variables – More RULES The official style guide for Python (yes, programmers really wrote a book on style) says that multi-word variable names in Python should be separated by underscores. For example, use …. hair_style and not hairStyle.
Naming Variables – Examples Either of these are fine with me ... I investigated and ALOT of Python developers use firstName as opposed to first_name
Exercises - Variables Complete Exercise 1.6 – Variables Typecasting