Variables
Variables store stored values in memory. When you declare a variable, a location in memory is opened to store its contents.
Based on the data type of the variable, the interpreter allocates memory space and determines what to store. Therefore, we can allocate different data types through variables, and the data types can be stored in variables as integers, decimals, characters, etc.
In python, variables do not need to explicitly declare the variable type and length to reserve memory space. When a variable is assigned a value, Python will automatically issue a statement. The equal sign (=) is used for variable assignment. Python variables do not need to be preceded by special symbols like PHP.
Precautions for using variables:
0. You must assign a value to a variable before using it
1. Variable names can only use English letters, underscores, and numbers. Variable names can start with letters and underscores, numbers cannot be used as the beginning
2. Variable names cannot contain spaces, but underscores can be used to separate words
3. Keywords in python cannot be used Words are used as variable names such as print, etc.
4. python variable names are case-sensitive, Name and name are two completely different names
= The left side of the operator is the variable name , the right side is the variable value, such as:
name = "Magic" #A string
age = 24 #An integer
miles = 123.56 #A floating point number (decimal)
print(name)
print(age )
print(miles)
Here the value string (magic), integer (24), floating point number (123.56) are respectively Assigned to name, age, miles, executing the code will produce the following:
Magic
24
123.56
At the same time, python allows simultaneous Assign a single value to multiple variables like:
a = b = c = 1
Here an integer object is created with value 1 and all three variables are assigned to the same memory location, You can also assign multiple variables to multiple values, such as:
a, b, c = 10, 11.5, "Magic"
Here a is assigned an integer value: 10, b is assigned a floating point number: 11.5, and c is assigned a string: magic.
Python’s five standard data types:
1. Number: The data type stores a numeric value, and when it is allocated, an object is created. Python supports three different numerical types:
int (signed integer)
float (floating point real value)
complex (complex number)
All integers in python3 are represented as long integers. Therefore, there is no separate numeric type for long integers.
2. String: A string in python is identified as a set of consecutive characters expressed in quotation marks. python allows double quotes and single quotes. You can use the fragment operators ([ ] and [ : ]) to obtain a subset (substring) of a string, whose indices start at index 0 at the beginning of the string and -1 represents the last character in the string .
3. List: The most versatile of python’s composite data types. A list contains items separated by commas and enclosed in square brackets ([ ]). Values stored in a list can be accessed using the slicing operators ([ ] and [ : ]), with indexing starting at 0 at the beginning of the list, and -1 for the last item in the list. The plus sign ( + ) is the list concatenation operator, and the asterisk ( * ) is the repetition operator.
4. Tuple: Tuple is another sequence data type that is very similar to a list. Tuples are multiple values separated by commas. However, unlike lists, tuples are enclosed in parentheses (( )). The main difference between list and tuple is - List is enclosed in brackets ([]
) and the elements and size in the list can be changed whereas tuple is enclosed in brackets (()
), cannot be updated. A tuple can be thought of as a read-onlylist
5. Dictionary: Python’s dictionary is a hash table type. They work like associative arrays or hashes found in Perl, consisting of key-value pairs. Dictionary keys can be almost any Python data type, but usually numbers or strings are used for convenience. On the other hand, the value can be any arbitrary Python object. Dictionaries are enclosed by curly brackets ({}
), and values can be assigned and accessed using square brackets ([]
).
Sometimes, it may be necessary to perform conversions between built-in types. To convert between types, just use the type name as a function.
There are the following built-in functions for performing conversion from one data type to another. These functions return a new object representing the converted value. They are as follows -
Number | Function | Description |
---|---|---|
int(x [,base])
| Convert x to an integer. If x is a string, base specifies the base.
|
|
float(x)
| Convert x to a floating point number.
|
|
complex(real [,imag])
| Create a complex number. ||
str(x)
| Converts object x to a string representation.
|
|
repr(x)
| Converts object x to an expression string.
|
|
eval(str)
| Evaluates a string and returns an object. ||
tuple(s)
| Convert s to a tuple.
|
|
list(s)
| Convert s to a list.
|
|
set(s)
| Convert s to a set.
|
|
dict(d)
| Create a dictionary, d must be (key, value) Sequence of tuples
|
|
frozenset(s) |
will | sConvert to frozen set
|
chr(x) |
Convert integer | xConvert to character
|
unichr(x) |
Convert integer | x to Unicode characters.
|
ord(x) |
Converts the single character | x to its integer value.
|
hex(x) |
Convert the integer | x to hexadecimal characters string.
|
oct(x) |
Converts the integer | x to an octal string.
|
The above is the detailed content of Introduction to learning variables in python. For more information, please follow other related articles on the PHP Chinese website!