Linux

Python Variables and Python Assignment Statement

Variable associates a name with a value. Through variable you can store information. Variables are nothing but reserved memory locations to store values. When a variable is created, it reserves some space in memory. Based upon the data type of a variable, the interpreter of Python allocates memory and decides what can be stored in the reserved memory. Therefore, the integers,decimals or characters can be stored by assigning different data types to variables, user need to be mindful while choosing names for your variables. Some name such as if, else, print etc are already reserved. Python Variable names can be lower case and uppercase. A Python variable can be made using letters,numbers and underscores, however Python variable can not start with a number. Python Variables receive their initial values and can be reset to new values with an Python assignment statement. The form of an Python assignment statement is the following:
<variable name>= < expression >

Example 1:

money = 100 # An integer assignment
meter = 1000.0 # A floating point
name = "mohit" # A string

Python Multiple Assignment

Python allows you to assign a single value to several Python variables simultaneously. For example:
a = b = c = 1

Python Numeric Data Types

Python allows programmers to use several different types of numbers. The two types used in this program are integer and floating point numbers. The integers include 0, all of the positive whole numbers, and all of the negative whole numbers The Python interpreter first checks the expression on the right side of the assignment operator and then binds the value with its variable name, it is called defining or initializing the Python variable. Python integer data type consists of the integers ranges from -2^31 to 2^31-1 the leading minus sign shows the negative values. When the integer's values exceeds these limit then Python interpreter automatically uses the long data type to and put "L" at the end of integer to represent it as shown in figure below.
Python long int
long int

Python Floating-Point Numbers

Python uses floating-point numbers to represent real numbers. Values of the most common.
  • Implementation of Python's float type range from approximately -10^308 to 10^308 and have 16 digits of precision [1].
  • Python floating-point number can be written using either ordinary decimal notation or scientific notation. Scientific notation is often useful for mentioning very large numbers as shown in figure below.
  • Python Scientific and Decimal Notation of float
    Python Scientific and Decimal Notation

Python Character Sets

Python characters look like string,the below figure shows the mapping of character set
Python Mapping of character values
Mapping of Python character values
The above figures is showing the mapping of first 128 ASCII codes to character values. The left column digits are the leftmost digit of ASCII code. The Top row's digits represent rightmost digits. For example to glean ASCII code of charcter "L" would be 76,

Python Conversion functions

If you want to convert character value to ASCII code use ord() function and if you want to convert ASCII code character use chr() function as shown in figure below.
Conversion functions ord() chr()
Conversion functions

Python Arithmetic Expressions

Python Arithmetic Expressions comprises operands and operators. Consider x and y are two operands having value 10 and 20 respectively.
OperatorDescription
**Exponent - Performs exponential (power) calculation on operators
*Multiplication - Multiplies values on either side of the operator
/Division - Divides left hand operand by right hand operand
%Modulus - Divides left hand operand by right hand operand and returns remainder
+Addition - Adds values on either side of the operator
-Subtraction - Subtracts right hand operand from left hand operandmark

The below figure shows the precedence order
Python operator Precedence order
Precedence order
The above figure shows the Exponent has high precedence and Addition and Subtraction has lowest precedence. If you want to change the order of evaluation use parentheses If operands of expression are of same data type, then resulting value is also that type. However addition of two ints data type can produce long int.

Python Mixed-Mode Arithmetic

The calculation which entails both integer and floating-point number is called Python mixed-mode arithmetic. When each operand is of a different type, the resulting value is of the more general type and float type is more general type. Below figure will clear you doubt.
python operator Mixed-Mode conversion
Python Mixed-Mode conversion
In above example 9/2.0, the less general type int (9) is temporarily and automatically converted into float (9.0) then calculation is performed.

Python Type Conversions

Python type conversion function converts the data type of operand. See the below figure.
python operator type conversion
Type conversion
In above figure 4.77 converts into 4. If you want convert 4.77 into int use int() function. If you want to convert into float use float() function.

[1] Fundamentals of Python: Data Structures by Kenneth Lambert







admin