Variables
Last updated
Was this helpful?
Last updated
Was this helpful?
Every Python variable has the following:
a name
a value (the content of the container
To give a variable a name some strict rules:
The name must be composed of upper or lower case letters, digits and the underscore character
The name MUST begin with a letter
The underscore character IS a letter
Upper and Lower case letters are not the same - Alice does NOT equal ALICE
The name of the variable cannot be any of Python's keywords
The PEP 8 -- recommends the following naming convention for variables and functions in Python
variable names should be in lowercase, with words separated by underscores
(eg var, my_variable
)
function names follow the same convention as as variable names (eg, fun, my_function
)
Its also possible to use mix case (eg myVariable) but ony in context where thats already the prevailing style.
False, None, True, and, as, assert, break, class, continue, def, del, elif, else, except, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, raise, return, try, while, with, yield
A variable comes into existence as a result of assigning a value to it. Unlike in other languages, you don't need to declare it in any special way.
If you assign any value to a nonexistent variable, the variable will be automatically created. You don't need to do anything else.
The creation (or otherwise - its syntax) is extremely simple: just use the name of the desired variable, then the equal sign (=) and the value you want to put into the variable.
A variable allows you to store a value by assigning it to a name, which can be used to refer to the value later in the program.
For example, in game development, you would use a variable to store the points of the player.
To assign a variable, use one equals sign.
user = "James"
You can use variables to perform corresponding operations, just as you did with numbers and strings:x = 7
print(x)
print(x + 3)
print(x)
Here is a short story:
Once upon a time in Appleland, John had three apples, Mary had five apples, and Adam had six apples. They were all very happy and lived for a long time. End of story.
Your task is to:
create the variables: john
, mary
, and adam
;
assign values to the variables. The values must be equal to the numbers of fruit possessed by John, Mary, and Adam respectively;
having stored the numbers in the variables, print the variables on one line, and separate each of them with a comma;
now create a new variable named totalApples
equal to addition of the three former variables.
print the value stored in totalApples
to the console;
experiment with your code: create new variables, assign different values to them, and perform various arithmetic operations on them (e.g., +, -, *, /, //, etc.). Try to print a string and an integer together on one line, e.g., "Total number of apples:"
and totalApples
.
john = 3
mary = 5
adam=6
totalApples=john+mary+adam
print (john, mary, adam)
3 5 6
print(totalApples)
14
If we use the same variable both to the right and left of the =
operator Python uses a shortcut for example:
x = x + 2
We can write it as:
x+=2
i = i +2*j
i+=2*j
var = var/2
var /=2
rem = rem % 2
rem % = 2
j = j - (i + var + rem)
j - = (i + var + rem)
x = x**2
x** = 2
a = 6
b = 3
a/=2*b
-------------
a = 2/a * 3
1.0 ---- Answer