Python basics-variables

Source: Internet
Author: User

1. What is a variable?

It's actually a name for the Data. In Python you don't want to care about the data type, because it helps you to recognize it when you're assigned a Value.

2. When creating a variable, a space is created in memory, and the details do not need to be cared for, The interpreter allocates the specified memory and determines what data can be stored in memory

Variables in 3.python do not need to be declared. each variable needs to be assigned before it is used, and the variable is created after the variable is Assigned.

4. Names of variables: letters, Numbers. underline;

Numbers cannot start; keywords cannot be used

5. Classification of Variables:

Mutable: list \ Dictionary

Immutable: number \ string \ tuple (array)

Counter = 100

Miles = 10000

Name = "xiaoming"

Print (counter)

Print (miles)

Print (name)

Multiple variable assignments, Too many are not recommended

A = b = C =1

A,b,c = "xiaoming"

String

" Ilovexiaoqiang " Print # Remove the value    from 0 to 4 Print (s[0:5]+'xiaoqiang'# string and string stitching print#  string repeats two times

List

1 #declaration of a list2List = ['ABCD', 786, 2.23,'Xiaoqiang', 70.2]3Tinylist = [123,'Xiaoqiang']4 5 Print(list)#Output6 Print(list[0])#Remove the value labeled 07 Print(list[1:3])#Remove the value from 1 to 28 Print(list[2:])#starting at subscript 2, until the last value9 Print(tinylist*2)#Repeating listTen Print(list+tinylist)#concatenation of lists and lists

Meta-group

tuple = (' abcd ', 786,2.23, ' Xiaoqiang ', 70.2)
List = [' abcd ', 786,2.23, ' Xiaoqiang ', 70.2]
#tuple [2] = #元组中是非法应用, immutable, Cannot change value
list[2] = #列表中是合法应用, variable, can change the value
Print (tuple)
Print (list)

Dictionary

Dictionaries and lists are similar, but there are different places
A list is an ordered collection of objects, and a dictionary is an unordered collection of objects
The elements in the dictionary are accessed by keys (similar to the Key,value form), rather than by offsets

dict = {' name ': ' Xiaoqiang ', ' code ': 6734, ' Dept ': ' Sales '} #字典

Print (dict)
Print (dict.keys ()) #打印出所有的key
Print (dict.values ()) #打印出所有的值
Print (dict.get (' name ', ' not found ')) #通过key取对应的value, if no corresponding key is found, the output is not found

Common data type conversions

int (xxxx)

STR () #强制转换成字符串

List () #强制转换成列表

Dict () #强制转换成字典

A = 100

Print ("xiaoqiang" +str (a))

str = "1111"

Print ("length is", len (str)) #去字符串的长度

Python basics-variables

Related Article

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.