Variables Flashcards

1
Q

But it’s quite a normal question to ask how to store the results of these operations, in order to use them in other operations, and so on.

How do you save the intermediate results, and use them again to produce subsequent ones?

Python will help you with that. It offers special “boxes” (containers) for that purpose, and these boxes are called

A

variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Variables hav

A

Name

Value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

variable naming rules

A

the name of the variable must be composed of upper-case or lower-case letters, digits, and the character _ (underscore)

the name of the variable must begin with a letter;

the underscore character is a letter;

upper- and lower-case letters are treated as different (a little differently than in the real world - Alice and ALICE are the same first names, but in Python they are two different variable names, and consequently, two different variables);

the name of the variable must not be any of Python’s reserved words (the keywords - we’ll explain more about this soon).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

variable recommendations

A

variable names should be lowercase, with words separated by underscores to improve readability (e.g., var, my_variable)

function names follow the same convention as variable names (e.g., fun, my_function)

it’s also possible to use mixed case (e.g., myVariable), but only in contexts where that’s already the prevailing style, to retain backwards compatibility with the adopted convention.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

[‘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’]

They are called keywords or (more precisely) reserved keywords

A

reserved because you mustn’t use them as names: neither for your variables, nor functions, nor any other named entities you want to create.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

A variable comes into existence as a result of assigning a value to it.

A

Unlike in other languages, you don’t need to declare it in any special way.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

If you assign any value to a nonexistent variable, the variable will be

A

automatically created. You don’t need to do anything else.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The creation (or otherwise - its syntax) is extremely simple:

A

just use the name of the desired variable, then the equal sign (=) and the value you want to put into the variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

you can put ANYTHING inside a variable.

A

just use the name of the desired variable, then the equal sign (=) and the value you want to put into the variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

var = 1
account_balance = 1000.0
client_name = ‘John Doe’
print(var, account_balance, client_name)
print(var)

You’re not allowed to use a variable which doesn’t exist (in other words, a variable that was not assigned a value).

A

This example will cause an error:

var = 1
print(Var)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

vars are case sensitive

A

We’ve tried to use a variable named Var, which doesn’t have any value (note: var and Var are different entities, and have nothing in common as far as Python’s concerned).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

You can use the print() function and combine text and variables using the + operator to output strings and variables, e.g.:

var = “3.8.5”
print(“Python version: “ + var)

A

Python version: 3.8.5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you assign a new value to an already created variable?

A

You just need to use the equal sign.

The equal sign is in fact an assignment operator. Although this may sound strange, the operator has a simple syntax and unambiguous interpretation.

var = 1
print(var)
var = var + 1
print(var)

The code sends two lines to the console:

1
2 - incremented value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly