Home  >  Article  >  Backend Development  >  Introduction to learning variables in python

Introduction to learning variables in python

零下一度
零下一度Original
2017-07-24 16:32:531328browse

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 ([]).

Data Type Conversion

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 -


##1Convert 2Convert 3Create a complex number. 4Converts object 5Converts object 6Evaluates a string and returns an object. 7Convert 8Convert 9Convert 10Create a dictionary, ##11frozenset(s)s12chr(x)x13unichr(x)x14ord(x)x15hex(x)x16oct(x)x
Number Function Description
int(x [,base]) x to an integer. If x is a string, base specifies the base.
float(x) x to a floating point number.
complex(real [,imag])
str(x) x to a string representation.
repr(x) x to an expression string.
eval(str)
tuple(s) s to a tuple.
list(s) s to a list.
set(s) s to a set.
dict(d) d must be (key, value) Sequence of tuples
willConvert to frozen set
Convert integer Convert to character
Convert integer to Unicode characters.
Converts the single character to its integer value.
Convert the integer to hexadecimal characters string.
Converts the integer 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn