Python Beginner's 2 variables

Source: Internet
Author: User
Tags numeric value

Variable

A variable is a stored value that is stored in memory. When declaring a variable, it creates a place in memory where the content is stored.

Based on the data type of the variable, the interpreter allocates memory space and determines what is stored. So we can assign different data types through the variables, we can store the data type as integers, decimals, characters, etc.

In Python, variables do not need to explicitly declare variable types and lengths to preserve memory space. When a variable is assigned a value, Python automatically declares that the equals sign (=) is used for the variable assignment, and the Python variable does not need to be preceded by a special symbol like PHP

Variables Use precautions:

0. You must assign a value to its first before using the variable

1. Variable names can only use English letters, underscores, numbers. Variable names can begin with letters and underscores, and numbers cannot be used as the beginning

2. Variable names cannot contain spaces, but you can use underscores to interval words

3. You cannot use keywords in python to make variable names such as print, etc.

4.python variable names are case-sensitive, name and name are completely different two names

The left side of the = operator is the variable name, and the right is the variable value, such as:

Name = "Magical" #一个字符串

Age = #一个整数

Miles = 123.56 #一个浮点数 (decimal)

Print (name)

Print (age)

Print (miles)

Here will be the value string (Magic), Integer (24), floating point number (123.56) is assigned to Name,age,miles, the execution code will produce the following:

Magic

24

123.56

At the same time, Python allows assigning a single value to multiple variables such as:
A = b = c = 1

This creates an integer object with a value of 1 and assigns all three variables to the same memory location, and multiple variables can be assigned to multiple values, such as:

A, b, C = 10, 11.5, "magical"

This assigns a value to an integer: 10,b is assigned a floating point number: 11.5,c is assigned to a string: magical.

Python Five standard data types:

1. Number: The data type stores a numeric value, and when assigned, creates an object. Python supports three different numeric types:

int (signed integer)

Float (floating point real value)

Complex (plural)

All integers in the python3 are represented as long integers. Therefore, long integers do not have a separate numeric type.

2. String: A string in Python is identified as a continuous character set represented in quotation marks. Python allows double and single quotes. You can use the fragment operator ([] and [:]) to get a subset of a string (a substring) whose index starts 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 composite data types. A list contains items that are separated by commas and enclosed in square brackets ([]). The values stored in the list can be accessed using the slice operator ([] and [:]), starting at 0 at the beginning of the list, and 1 representing the last item in the list. The plus sign (+) is the list join operator, and the asterisk (*) is a repeating operator.

4. Tuple: Another sequence data type in which tuples are very similar to lists. Tuples are separated by multiple values by commas. Unlike lists, however, tuples are enclosed within parentheses (()). The main difference between a list and a tuple is that the list is enclosed in parentheses ( [] ), the elements and sizes in the list can be changed, and tuples are enclosed in parentheses ( () ) and cannot be updated. Tuples can be thought of as read-only lists

5. Dictionary: A python dictionary is a type of hash table. They work like associative arrays or hashes found in Perl, and consist of key-value pairs. The dictionary key can be almost any Python data type, but it is often used to facilitate the use of numbers or strings. On the other hand, the value can be any arbitrary Python object. {}the dictionary is enclosed in curly braces (), and you can use square brackets ( [] ) to assign and access values.

Data type conversions

Sometimes, you might need to perform conversions between built-in types. To convert between types, simply use the type name as a function.

There are several built-in functions for performing conversions from one data type to another. These functions return a new object that represents the converted value. They are shown below-

numbering function Description
1 int(x [,base]) is x converted to an integer. If x it is a string, you base specify the cardinality.
2 float(x) will be x converted to floating-point numbers.
3 complex(real [,imag]) Creates a complex number.
4 str(x) Converts an object x to a string representation.
5 repr(x) Converts an object x into an expression string.
6 eval(str) Evaluates the value of a string and returns an object.
7 tuple(s) will be s converted to a tuple.
8 list(s) will be s converted to a list.
9 set(s) will be s converted to a collection.
10 dict(d) Create a dictionary that d must be a (key,value) sequence of tuples
11 frozenset(s) sConvert to Frozen set
12 chr(x) Convert an integer x to a character
13 unichr(x) Converts an integer x to a Unicode character.
14 ord(x) Converts a single character x to its integer value.
15 hex(x) Converts an integer x to a hexadecimal string.
16 oct(x) Converts an integer x to an octal string.

Python Beginner's 2 variables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.