Variables - Python

From TRCCompSci - AQA Computer Science
Revision as of 09:07, 20 June 2018 by Admin (talk | contribs) (Created page with "=What is a Variable= A variable can be used to store a value within your program. Most languages require you to declare a variable and then assign a value to it, however pytho...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

What is a Variable

A variable can be used to store a value within your program. Most languages require you to declare a variable and then assign a value to it, however python does not.

In most languages, a variable is of a specific data type and this can't change during the program. However python will determine the data type to use from the data stored in the variable.

Variables in Python

You need to give a name for the variable and then assign it a value:

counter = 100 # An integer assignment 
miles = 1000.0 # A floating point 
name = "John" # A string

You could print the current values for each variable:

counter = 100 # An integer assignment 
miles = 1000.0 # A floating point 
name = "John" # A string 
print("counter value is " + counter)
print("miles value is " + miles)
print("name value is " + name)