0% found this document useful (0 votes)
15 views

Python Note 4

Personal Python Learning Note

Uploaded by

brian07151992
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Python Note 4

Personal Python Learning Note

Uploaded by

brian07151992
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Python Note 4

30/12/2023

What are variables?

How to store the results of these operations, in order to use them in other
operations, and so on.

Python will help you with that. It offers special "boxes" (containers) for that
purpose, and these boxes are called variables - the name itself suggests that
the content of these containers can be varied in (almost) any way.

What does every Python variable have?

 a name;
 a value (the content of the container)

If you want to give a name to a variable, you must follow some strict rules:

 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).

Correct and incorrect variable names

Note that the same restrictions apply to function names.

Python does not impose restrictions on the length of variable names, but that
doesn't mean that a long variable name is always better than a short one.
Here are some correct, but not always convenient variable names:

MyVariable , i , t34 , Exchange_Rate , counter , days_to_christmas , TheNameIs


SoLongThatYouWillMakeMistakesWithIt , _ .

Moreover, Python lets you use not only Latin letters but also characters
specific to languages that use other alphabets.

These variable names are also correct:

Adiós_Señora , sûr_la_mer , Einbahnstraße , переменная .

And now for some incorrect names:

10t (does not begin with a letter), Exchange Rate (contains a space)

NOTE

The PEP 8 -- Style Guide for Python Code recommends the following naming
convention for variables and functions in Python:

 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.

Keywords
Take a look at the list of words that play a very special role in every Python
program.

['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. They are
reserved because you mustn't use them as names: neither for your
variables, nor functions, nor any other named entities you want to create.

The meaning of the reserved word is predefined, and mustn't be changed in


any way.

Fortunately, due to the fact that Python is case-sensitive, you can modify any
of these words by changing the case of any letter, thus creating a new word,
which is not reserved anymore.

For example - you can't name your variable like this:

import

You mustn't have a variable named in such a way - it is prohibited. But you
can do this instead:

Import

These words might be a mystery to you now, but you'll soon learn the
meaning of them.

These words cannot be used as identifiers for variables, functions,


classes, or any other user-defined item.

Creating variables

What can you put inside a variable?


Anything.

You can use a variable to store any value of any of the already presented
kinds, and many more of the ones we haven't shown you yet.

The value of a variable is what you have put into it. It can vary as often as you
need or want. It can be an integer one moment, and a float a moment later,
eventually becoming a string.

Let's talk now about two important things - how variables are created,
and how to put values inside them (or rather - how to give or pass
values to them).

REMEMBER

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.

Take a look at the snippet:

var = 1
print(var)

It consists of two simple instructions:

 The first of them creates a variable named var , and assigns a literal
with an integer value equal to 1 .
 The second prints the value of the newly created variable to the
console.
Note: print() has yet another side to it - it can handle variables too. Do you
know what the output of the snippet will be?

Assigning a new value to an already existing

variable

How do you assign a new value to an already created variable? In the same
way. 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.

It assigns the value of its right argument to the left, while the right argument
may be an arbitrarily complex expression involving literals, operators and
already defined variables.

Look at the code below:

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

The code sends two lines to the console:

1
2

The first line of the snippet creates a new variable named var and
assigns 1 to it.

The statement reads: assign a value of 1 to a variable named var .


We can say it shorter: assign 1 to var .

Some prefer to read such a statement as: var becomes 1 .

The third line assigns the same variable with the new value taken from
the variable itself, summed with 1 . Seeing a record like that, a mathematician
would probably protest - no value may be equal to itself plus one. This is a
contradiction. But Python treats the sign = not as equal to, but as assign a
value.

So how do you read such a record in the program?

Take the current value of the variable var , add 1 to it and store the result in
the variable var .

In effect, the value of variable var has been incremented by one, which has
nothing to do with comparing the variable with any value.

Do you know what the output of the following snippet will be?

var = 100
var = 200 + 300
print(var)

500 - why? Well, first, the var variable is created and assigned a value of 100.
Then, the same variable is assigned a new value: the result of adding 200 to
300, which is 500.

Shortcut operators

It's time for the next set of operators that make a developer's life easier.

Very often, we want to use one and the same variable both to the right and
left sides of the = operator.

For example, if we need to calculate a series of successive values of powers of


2, we may use a piece like this:
x = x * 2

You may use an expression like this if you can't fall asleep and you're trying to
deal with it using some good, old-fashioned methods:

sheep = sheep + 1

Python offers you a shortened way of writing operations like these, which can
be coded as follows:

x *= 2
sheep += 1

Let's try to present a general description for these operations.

If op is a two-argument operator (this is a very important condition) and the


operator is used in the following context:

variable = variable op expression

It can be simplified and shown as follows:

variable op= expression

Take a look at the examples below. Make sure you understand them all.

i = i + 2 * j ⇒ i += 2 * j

var = var / 2 ⇒ var /= 2

rem = rem % 10 ⇒ rem %= 10

j = j - (i + var + rem) ⇒ j -= (i + var + rem)

x = x ** 2 ⇒ x **= 2

Key takeaways
1. A variable is a named location reserved to store values in the memory. A
variable is created or initialized automatically when you assign a value to it
for the first time. (2.1.4.1)

2. Each variable must have a unique name - an identifier. A legal identifier


name must be a non-empty sequence of characters, must begin with the
underscore( _ ), or a letter, and it cannot be a Python keyword. The first
character may be followed by underscores, letters, and digits. Identifiers in
Python are case-sensitive. (2.1.4.1)

3. Python is a dynamically-typed language, which means you don't need


to declare variables in it. (2.1.4.3) To assign values to variables, you can use a
simple assignment operator in the form of the equal ( = ) sign, i.e., var = 1 .

4. You can also use compound assignment operators (shortcut operators)


to modify values assigned to variables, e.g., var += 1 , or var /= 5 * 2 .
(2.1.4.8)

5. You can assign new values to already existing variables using the
assignment operator or one of the compound operators, e.g.: (2.1.4.5)

var = 2
print(var)

var = 3
print(var)

var += 1
print(var)

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

var = "007"
print("Agent " + var)

You might also like