Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this.

Similar presentations


Presentation on theme: "Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this."— Presentation transcript:

1 Variables and Strings

2 Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this value a name e.g. firstName  This is called a variable  A variable is a named area of memory that is used to hold some data whilst the program is running  We use the = assignment operator to put a value into this variable. E.g. firstName = “Susan”sport = “swimming” count = 10rate = 12.2 2 of 18

3 Evaluating an Expression  These are all assignment expressions: value = amount * rate total = total + number mark = total / 100 Work out (evaluate) the part on the right-hand side of the = sign Assign the result to the variable on the left 3 of 18 value 150 If amount = 10 and rate = 15…

4 Data Types  A data type is a classification of the sort of value being represented.  A string is something in double or single quotes  An integer is a whole number  A floating point number has a decimal part  A boolean is either True or False 4 of 18

5 Displaying the Contents of a Variable  We can display the value being held in a variable:  Or do an operation first…  Try some examples…  Don’t forget to put something in the variables before you try to print them out… 5 of 18

6 Variables  Creating a new variable is called declaring a variable  You don’t need to tell Python what type of data you are going to use a variable for  However, it is poor practice to change it once you’ve started to use it  This will result in an error. 6 of 18

7 Identifiers  Names for variables, functions etc. are called identifiers  There are some rules for naming them: You cannot use special characters (e.g. space, &, !, $, * etc.) You cannot start an identifier with a number (e.g. 1stname) You cannot use reserved keywords (more of these later)  Variable names should be descriptive but not too long  These are all good examples: first_name, discountedPrice, favourite_sport  These are not: a, f1, the_users_favourite_sport 7 of 18

8 Exercises 1. Declare and initialise these variables: name, team Display a message like this: My friend supports 2. Declare and initialise these variables: favFilm, reason Display a message like this: I enjoyed because 3. Declare and initialise two variables to hold your friend’s name and age. Display a message showing your friend’s name and age. 8 of 18

9 Changing the Contents of a Variable firstName = “Susan” count = 10 total = 5 We can change a variable by:  Simply replacing the old value, e.g. firstName = “Fred” count = 12 total = 12 + 11  Incrementing it if it’s a number, e.g. count = count + 1 9 of 18

10 What can we do with strings?  A string isn’t just text, it has functionality…  A string can Tell you what it would look like in a different case… Try these… Here, we are calling the upper() function on the myText string variable 10 of 18

11 Be Careful…  What does variable name contain now?  The upper(), lower() etc. string functions do not change the value of the variable. How can we do this? 11 of 18

12 Exercise Declare and initialise two variables: firstName and surname Convert the surname to upper case Convert the firstName to title case Print them like this:, 12 of 18

13 What else can we do with strings?  Join strings together using a +. This is called concatenation.  You need to explicitly add a space between parts if necessary 13 of 18

14 Exercises 1. Cars Create two variables: colour and carModel and give them values. Join these strings together into a new variable called carDesc e.g. if colour is “red” and carModel is “VW Golf” then carDesc should contain“red VW Golf” 2. Shopping List Create three variables: item1, item2 and item3 and give them values Create a new variable: list which contains “Don’t forget to buy:” followed by all the items e.g. Don’t forget to buy: cheese, bread and tomatoes 14 of 18

15 Concatenation and Addition  Have a look at this program. What will be printed?  How about this one?  Why are they different?  What if one is a string and one is a number??! Try it… 15 of 18

16 More about Strings  We can also find out the number of characters in a string  Why are these used differently?  This is because the function len() can be used with things other than strings. The function upper() is specific to strings and is this functionality is part of what it is to be a string... 16 of 18

17 Exercise for Next Week 1. Store your first name in a variable 2. Store your surname in a different variable 3. Change the surname to uppercase 4. Display your full name in this format: SURNAME Firstname 5. Create a new variable called fullName 6. Concatenate your firstname and surname and store the result in fullName 7. Display the value of fullName 8. Display the length of the fullName 17 of 18

18 Summary  We have looked at: What variables are and why we use them How to evaluate an expression Datatypes Different things we can do with strings  For next week: Complete the exercise on the previous slide 18 of 18


Download ppt "Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this."

Similar presentations


Ads by Google