Download presentation
Presentation is loading. Please wait.
Published byHubert Simon Modified over 5 years ago
1
Variables In today’s lesson we will look at: what a variable is
different types of variables why we have types using variables in your program
2
Variables Variables are probably the most important thing in programming. They store the data that your program is using as it runs. Without them, it would be very difficult (if not impossible) to write a useful program. There are similarities between the way variables are used and algebra.
3
Types With most programming languages (but not all of them) variables have a type. Python has four main types – integer (whole number), float (number), Boolean (true or false) and string (text) The type of a variable in Python is set automatically based on the value you put in it, but you can force, e.g., a number to be stored as a string if you want to.
4
Variable Names Variable names are words or letters – just like algebra
You can use any combination of letters and numbers, but variable names: must begin with a letter contain only letters and numbers must not be commands, e.g. PRINT is not a valid variable name
5
Examples What would be a good name for a variable to store the following data: Someone’s name Someone’s age The cost of postage A postcode The total price for an item A telephone number
6
Assignment You must give a variable a value before you use it, e.g. setting a score to zero. Giving a variable a value is know as assigning a value. You can use a single value or the result of a calculation, e.g. age = 10 age = 8 + 2 name = "Joe" fullname = "Joe " + "Bloggs" fullname = forename + " " + surname
7
Assignment age = age + 1 (age += 1 does the same)
You can also use the same variable as part of the assignment - this is useful when adding to, or taking away from, a value: age = age + 1 (age += 1 does the same) fullname = "Mr " + fullname The following assignments is NOT valid and will result in a type mismatch error because you're trying to mix strings and numbers: name = “Andrew" name = name + 10
8
Examples name = “Andrew” print(name) name = “Hello ” + name
What would be the output of this program? name = “Andrew” print(name) What about this one? name = “Hello ” + name
9
Examples What would be the output of this program?
print a + b What are the values of a and b at the end? a = 4 b = 2 a = a + b b = b + 1
10
Casting Casting means changing the type of a variable
Python will sometimes cast automatically (also known as coercion), e.g. to store the result of a calculation - sometimes you will get a type mismatch error or an unexpected result. e.g. if a = 1 and b = 2.5, a is an int to start with. Assigning a = a * b will make a equal to 2.5, and a becomes a float. But, if c = 1 and d = c/2, the value of d is 0 because d is also an int. However, e = d/2.0 would make e a float with the value 0.5!
11
Scope Variables declared in the main body of your program can be used anywhere in the program – these are called global variables Variables declared in a function can only be accessed in that function – these are called local variables. If you want to use a global variable in a function, use the global keyword
12
Constants Some programming languages have things called constants
Constants are named values, like variables, but their value doesn’t change They can be used to simplify the use of mathematical constants, e.g. pi = Python doesn’t have constants, but you can use variables in the same way
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.