Understanding Python Variables Through Metaphors and Examples

Slide Note
Embed
Share

Learn about variables in Python through a series of engaging metaphors and practical examples. Understand how variables are used to simplify complex expressions, the importance of assigning values to variables, and how to avoid errors in variable usage.


Uploaded on Sep 22, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. TeachingLondon Computing Topic 1.3 Python Variables William Marsh School of Electronic Engineering and Computer Science Queen Mary University of London

  2. Aims Understand the idea of a variable Using metaphors to explain variables Using variables to breakdown complex expressions Assignment statements Stages of understanding variables

  3. Program Variable Which are the following everyday values are like variables? Understanding 1: Variables is a name for a value Names are really important Name meaning Your height Your age The credit on your oyster Value can change

  4. Using a Variable A variable can be used instead of a value: greeting = "Hello" planet = "World" print(greeting, planet) The output is: 'Hello World' The variables are: greeting and planet Any names

  5. Decomposition using Variable How to break a complex calculation down into simpler steps? Recurring question in programming >>> km_mile = (1760 * 36 * 2.54) / 100 / 1000 >>> km_mile 1.609344 >>> inch_mile = 1760 * 36 >>> cm_mile = inch_mile * 2.54 >>> m_mile = cm_mile / 100 >>> km_mile = m_mile / 1000 >>> km_mile 1.609344

  6. Errors A variable must be given a value before it is used: >>> area = length * width Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> area = length * width NameError: name 'length' is not defined Notice: The message is complex read it carefully Only the first error is mentioned

  7. Assignment The statement to set a variable is call assignment >>> x = 10 can be read as 10 is assigned to x How do you read? >>> x = y Warning: assignment is not equals

  8. Assignment II x is assigned the value of y or the value of y is assigned to variable x >>> x = y How are the two variables used? Variable x changes value; the value of y stays the same The value of y is used (or read); an error occurs if y has never been given a value. It does not matter if x has no value before the statement; if it does, it is lost

  9. Quiz I What is the value of x after these statements have been executed? x = 5 x = x + 3 You can check the answer using the Python shell

  10. Variable as a Memory Location Understanding 2: A variable is a location in the computer s memory Name Value "CS" subject number 4 Python takes care of where to put the values activity FUN

  11. Quiz II What is the values of x and y after these statements have been executed? Each box is a separate problem x = 3 y = 2 x = x + y x = 3 y = 2 + x x = x + y x = 3 y = 2 x = x + y y = x 2 Which understanding is needed? You can check the answers using the Python shell

  12. Summary A variable gives a name to a value Choose a meaningful name Reading and Assignment A variable can be read in an expression A variable can be changed in an assignment statement Use variables To replace a complex expression with several simpler ones To hold an input string from the user We already did this

Related


More Related Content