WHAT IS VARIABLES AND ASSIGNMENT STATEMENTS IN PYTHON PROGRAMMING

VARIABLES AND ASSIGNMENT STATEMENTS

You’ve used IPython’s interactive mode as a calculator with expressions such as
In [1]: 45 + 72
Out[1]: 117
Let’s create a variable named x that stores the integer 7:
In [2]: x = 7
Snippet [2] is a statement. Each statement specifies a task to perform. The preceding
statement creates x and uses the assignment symbol (=) to give x a value. Most statements stop at the end of the line, though it’s possible for statements to span more than one line. The following statement creates the variable y and assigns to it the value 3:

In [3]: y = 3

You can now use the values of x and y in expressions:

In [4]: x + y

Out[4]: 10

Calculations in Assignment Statements

The following statement adds the values of variables x and y and assigns the result to the variable total, which we then display:

In [5]: total = x + y

In [6]: total

Out[6]: 10

The = symbol is not an operator. The right side of the = symbol always executes first, then the result is assigned to the variable on the symbol’s left side.

Python Style

The Style Guide for Python Code helps you write code that conforms to Python’s coding conventions. The style guide recommends inserting one space on each side of the assignment symbol = and binary operators like + to make programs more readable.

Variable Names

A variable name, such as x, is an identifier. Each identifier may consist of letters, digits and underscores (_) but may not begin with a digit. Python is case sensitive, so number and Number are dif erent identifiers because one begins with a lowercase letter and the other begins with an uppercase letter.

Types

Each value in Python has a type that indicates the kind of data the value represents. You can view a value’s type with Python’s built­in type function, as in:
In [7]: type(x)
Out[7]: int
In [8]: type(10.5)
Out[8]: float
The variable x contains the integer value 7 (from snippet [2]), so Python displays int
(short for integer). The value 10.5 is a floating­ point number, so Python displays
float.

*

إرسال تعليق (0)
أحدث أقدم