1.python Basic data types

Source: Internet
Author: User

1. Value type (4 kinds)

The numeric types are immutable

1) integral type (int)

Example

A = 0

b =-1

And all that, the whole type.

2) float type (float)

Example

F = 1.1

F =-1.2

3) Boolean type (BOOL)

Example

result = True

result = False

4) Complex Type (complex)

Example

c = 1+2j

2. Numerical operation (+-*/% *)

Example

A = 2
b = 2.5
A + B # 4.5
A-B #-0.5
A * b # 5.0 Note Floating-point multiplied by integral type, result is floating-point type
A/b # 0.8
a//b # (evenly divisible, rounded down)
2% #取余
9**2 # 9 Squared
3**3 # 3 of 3 power

3. Sequence type (3 kinds) 1) string (str)

The string is immutable (after the string is modified, a new string is generated and the ID is changed)

Example

s = ' Hello '

s = "Nihao" #字符串里面有单引号的时候, with double quotation marks

s = "Jack

Rose ' #三引号时可以换行, also used to annotate

s = "" "

Http://www.baidu.com/?wd=python

&ie=utf-8

"""

2) Ganso (tuple)

Ganso not variable

Tu = (1,2,3,4)

Tu = #不加括号也可以

Tu = (1,) #只有一个元素时, pay attention to the comma, or it will be treated as an integral type

3) lists (list)

The list is mutable (change some elements, the ID remains the same)

Example

Li = [] #空列表

Li = [1,2,3,4] #[] is a list of flags

Li = [(1), ' Hello ', true,[2,3]]

4) index, slice, step index:

Sequence types are ordered, so each element has a positional relationship in the sequence, and the index is equivalent to a number of elements, and the index starts at 0.

Example

Li =[1,2,3,4,5]
Tu = (1,3,4,5)
S= ' ASD '
LI[0] #取出第一个元素, index is starting from 0

Forward Index
LI[1]
TU[2]
S[0]
Reverse index,-1: Take out the penultimate one. -2: Take out the penultimate one
LI[-1]
S[-1]
Tu[-2]

Slice: Take out a small segment (left closed right open)

Li[0:2] Remove the first and second elements, the left border can be taken, the right border cannot be fetched
S[1:2] Take the second element out of the
Tu[1:3] Take out the second and third elements

Li[:3] Default from the first element (that is, index 0) to start fetching

Tu[1:] Default to Last (indexed to-1)

Stride Length: The element is taken once, and the sign indicates the direction

The default step size is 1

LI[::2] Every 2 steps, remove the element

Li[1:4:3] Every 3 steps, remove the elements from the Li index position from 1 to 4 (excluding 4)

Negative step: From right to left, every number of steps to take the element
TU[3:1:-1]
Tu[3:1:-2]

4. Assignment operation

Li *= 2 equals li = Li
A +=1 equivalent to a = a + 1

A +=1 equivalent to a = a + 1
A/= 4 equals a = A/4

T%= 3 equals t = t%3

Note in the middle of the symbol (+=,-=,*=,/=,%=) do not add a space, because this is a symbol

5. The member operation in determines whether the

The result is a Boolean value

s = ' ASD '

' s ' in S # True
' m ' in S # False

' s ' not in S # False
' m ' not in S # True

6. Supplement

1) = for complex value, = = To determine whether the value is equal, = = = Determine whether the object is the same (that is, the ID is consistent)

2)

View Python Keywords

Import keyword
Keyword.kwlist

Or

Help (keywords)

View Python built-in functions

Dir (__builtins__)

3) naming rules for variables

1. Variable name must be, letter, underscore, number composition
2. Cannot start with a number
3. Cannot use keywords

7. Homework

#1. Today, I learned to declare a variable, a = 42, and then do you have an error? X=y=z=1 will you get an error? (Verify right and wrong)



#2. Declares two variable a=1,b=2. How do I swap the values of two variables to make a=2,b=1? Please use the code to complete.


#3. Briefly describe the naming rules for the following variables.


#4. There is a time form (20170608), by dividing and taking the remainder, to get the corresponding day, month, year. Please use the code to complete.


#5. For a list of length 5, use a number of methods to take the 3rd digit value.

1.python Basic data types

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.