Introduction to Python Data Types and Variables ICS 3U0 Today’s Agenda Variables Declaring and Assigning Variables Data Types 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. is stored in memory. A variable is a specific location in memory. It is called a Information 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/double – 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 42 Example - Integers 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 a = 8/3 b = 6-7/9 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 – More RULES However, we, like most programmers, will use “camelCase”. That is, using a capital letter to distinguish each additional word in a variable. Variables start with a lower case Classes start with a capital letter (next year :) For example, use …. hairStyle for variable and HairStyle for a class 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 Complete Exercise 1.6 – Variables Typecasting