Python basic data type, python Data Type

Source: Internet
Author: User

Python basic data type, python Data Type

Variables in Python do not need to be declared. Each variable must be assigned a value before it can be used.

In Python, a variable is a variable and a memory address pointer. It has no type. What we call "type" is the type of the object in memory referred to by the variable. Similar to reference types in other java and C.

Python3 has six standard data types:

String)
Boolean)
Integer)
Float)
Digit (Digit)
List)
Tuple)
Sets)
Dictionary)
Date)

 

I. String)

String 'str' in python is enclosed by single quotation marks ('') or double quotation marks (" "), and special characters are escaped using a backslash.

>>> s = 'Yes, he doesn\'t'>>> print(s, type(s), len(s))Yes, he doesn't <class 'str'> 15

If you do not want to escape \, you can add r or R before the string to indicate the original string:

>>> print('C:\some\name')C:\someame>>> print(r'C:\some\name')C:\some\name

In addition, \ can be used as a red line break, indicating that the next line is also a continuation of the previous line. You can also use '''... ''' or ""... "to span multiple rows.

You can concatenate a string with the + operator or use the * operator to repeat the string:

Note: + when connecting strings, the memory is re-opened every time one is connected. If there are too many connections, the efficiency is not high. We recommend that you use a formatted string at this time.

>>> s1 = 'we'>>> s2 = 'are'>>> s3 = 'go'>>> print(s1 + ' ' + s2 + ' ' + s3)we are go>>>>>> print("%s %s %s" % (s1, s2, s3))we are go>>>>>> print('str' + 'ing', '*'*10)string **********

There are two indexing methods for strings in python: first, increasing from left to right and from 0, and then decreasing from right to left and from-1.

Note! There is no separate character type. A character is a string with a length of 1.

>>> word = 'Python'>>> print(word[0], word[5])P n>>> print(word[-1], word[-6])n P

You can also slice strings to obtain a substring. Use a colon to separate two indexes, in the form of a variable [head Subscript: end subscript]

The Truncation range is semi-closed and then open, and both indexes can be omitted:

>>> Word = 'ilovepython '>>>>>>>>> word [] 'love' # Jump between two >>> word [] 'lvpto' >>> word [:] 'ilovepython '>>> word [5:] 'python' >>> word [-10:-6] 'love'

Different from the C string, the python string cannot be modified. If a value is assigned to an index, word [0] = 'M' may cause an error.

Note:

* 1. The backslash can be used for escape, and r can be used to prevent the backslash from being escaped.

* 2. Strings can be connected together using the + operator, which is repeated using the * operator.

* 3. Two indexing methods for strings in python, starting from left to right with 0 and starting from right to left with-1

* 4. the strings in Python cannot be changed.

 

Ii. boolean)

Bool has only two values: True and False.

>>> type(True)<class 'bool'>>>> type(False)>>><class 'bool'>>>> bool(0)False>>> bool(1)True>>> bool(2)True>>> bool(3 == 3)True

Iii. integer)

 

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.