What & #39; s the python variable, basic data type, whatpython

Source: Internet
Author: User

What's the python variable, basic data type, whatpython

What's the variable

The type declaration is not required for variable assignment in Python.

Variables are created in the memory, including the variable identifier, name, and data.

A variable must be assigned a value before it can be used.

Equals sign (=) is used to assign values to variables.

  Variable definition:Name = Jack name is the variable name, and Jack is the variable value.

In python, values can be assigned to multiple variables at the same time, as shown in figure

a=b=c=1#a=1,b=1,c=1a,b,c=1,2,3#a=1,b=2,c=3

Considerations for declaring Variables

Comparison of variables:

Is compares IDs. If the IDs are the same, the data points to the same memory address, and all data is the same. = Compare values. If the values are the same, the data is not the same.

 

Program Interaction:Interaction between programs and users. For example, entering the account password on the QQ interface to log on is a type of program interaction.

Execution output

name = input("What is your name?")age = input("How old are you?")hometown = input("Where is your hometown?")print("Hello ",name , "your are ", age , "years old, you came from",hometown)

If the code is complex, you can add a comment after the corresponding code to avoid mistakes or forgetting, add # after a single line comment for the code, and then enter the comment, multiple-line comments are enclosed by three quotation marks ''''''

 

Basic Data Type

 

Basic Data TypeIt can be divided into numbers, string str, list, Dictionary dict, yuanzu tuple, set, Boolean Value

Data types are dividedVariable data typeAndUnchangeable Data Type.

Variable data type: the value can be changed without changing the id, such as list, Dictionary, ancestor, and set.

Immutable data type: the id also changes when the value is changed, such as numbers, strings, and Boolean

All data types in python are classes. You can view the data types of this variable through type:

Note: a mutable type can also be called a non-hash type. an unchangeable type can also be called a hash type.

 

Number:It can be divided into integer int type and floating point type float type.

Integer int (in integer form): level = 20;

Float (with decimal point): weight = 65.5 price = 13.5, etc.

 

String str:

  Descriptive data must be enclosed in quotation marks (in english format, no matter whether single or double quotes are used) name = 'jack', sex = 'male'

Concatenates strings, such

msg1='hello'msg2=' world'msg3=msg1+msg2print(msg3)#'hello world'

 

List:

Contained in [], the internal elements can be strings, numbers, lists, ancestor, set, and dictionary, separated by commas.

Purpose -- store multiple values, which can be modified

List1 = [3, 2, 1, 'Hello'] # Add the list1.append ('World') element # [3, 2, 1, 'Hello ', 'World'] # Delete the list1.remove (3) # [, 'Hello', 'World'] del list [2] # [1, 'Hello ', 'World'] # modify the element value. 0 in brackets indicates the index, representing the first position. The index in python is list1 [0] = 8 # [8, 'Hello', 'World']

 

Dictionary dict (unordered ):

Defined in {}, separated by commas (,). Each element is in the form of key: value.

Purpose -- store multiple values. This is the same as the list. The value can be of any data type.

Feature: each value corresponds to a key, which must be an unchangeable type: string or number.

Dic = {'name': jack, 'age': 17, 'hobby': ['girls', 'money ', 'delimiter']} # Add dic ['sex '] = 'male' # Delete del dic ['age'] # modify dic ['age'] = 18

 

Yuanzu tuple:

You can store multiple values. The ancestor is of an immutable type and is more used for query. The ancestor is written in a similar way as the list, but changed [] (). The ancestor can also be used as the dictionary key.

t=(1,[1,3],'sss',(1,2)) #t=tuple((1,[1,3],'sss',(1,2)))print(type(t))#<class 'tuple'>

Although the Ancestor Type is immutable, as long as the id of the element inside the ancestor does not change, the internal element change is allowed, such as the element whose list is the ancestor, the element in the list changes but the id remains unchanged.

 

Set:

Defined in {}. It can contain multiple elements separated by commas. The element of the set must be of an unchangeable type, and duplicate elements are not displayed in the Set (the Set has the automatic deduplication function). The set is unordered.

 

Boolean value:

There are only two types of Boolean values: True and False. The Boolean value is used for judgment.

 

Summary:

Variable assignment does not need to declare the type, which is very useful.

 

The numeric type can be an integer or floating point type, which is an unchangeable data type. If you change the numeric data type, a new object is assigned.

 

The list is identified by "[]", and internal elements are separated by commas.

 

Tuples are identified by "()", and internal elements are separated by commas. However, tuples cannot be assigned values twice, which is equivalent to read-only lists.

The set is identified by "{}". Internal elements are separated by commas. duplicate elements are not displayed in the set, and the set is unordered.

 

The dictionary is identified by "{}". The dictionary consists of the index key and its corresponding value. The dictionary is unordered.

The list can be directly assigned values. The tuples cannot be assigned values. Dict [k] = v .

The index subscript starts from 0.

 

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.