Python Basic Learning Path No.2 data type

Source: Internet
Author: User

Common data types in Python are: integers, floating-point numbers, strings, lists, tuples, dictionaries

Python can omit declarations from other languages, and can directly define assignment usage.

For example:

A=12 is equivalent to int a=12 omitted in other languages int of declared data type

? The following examples define variables of each type:

1. Define an integer (int)

A = 1

2. Define a floating-point type (float)

B = 1.2

3. Define a String

c = "Hello" (Note: there is no single quotation mark in Python, which can also be written as: c = ' Hello ')

4. Defining a list (list) in--python similar to, but not identical to, the concept of arrays in other languages

D = [2,2.1,3.14, "Test", "Thankyou"]

5. Define a meta-ancestor (tuple)

E = (3,7,1, "O", ' m ')

6. Define a dictionary (dict)

f = {"name": "Anges", "Password": "111"}

? variable assignment

1. Variable re-assignment (read from right to left)

Define A=1, give a re-assignment "Hello", print out the result is

2. Multi-variable assignment

You can assign values to multiple variables by "=".

You can also use "=" to implement two or more variables of the Exchange assignment, of course, multi-variable exchange assignment, need strong logical thinking to support.

? integer and floating-point arithmetic

1. Subtraction operation

Subtraction can be calculated by assigning values, or at output

Example: a = B + C print A/C

2. Taking the remainder

A = 17 to get the remainder of a divided by a number, you can use the "%" operator to implement

b = The value of a%5 B is the remainder of a

3. Floating point number

Floating-point numbers can be added and reduced with integers

4. Take power

Power, can be implemented with "* *" + several powers

2**3=2*2*2=8

? string operations

String only "+" and "*"

The "+" symbol, which is to connect two strings together

"*" Symbol, you can control the number of times the string output

? string slices

A = "Hello world!"

A[1:5]: Take 1-5

A[5:]: Take 5 to the last

A[:5]: Take start to 5

A[5]: Take a fifth character

A[::-1]: string inversion

A[6:3:-3]: Strings are reversed from 6 to 3

? simple string function

Len (string name): Calculate string length

String name. Count ("E"): Number of occurrences of character E

? boolean bool

Programming language, not 0 is true, 0 is false, none and Null characters are also false

? Logical operation

With: And two or more than two conditions all meet

Or: or two or more than two conditions have a satisfying

Non: Not one, two or more than two conditions are not satisfied

? list lists

Common operation is normal and delete and change the search

l=[1,7,3,4,1,3]

L.append (data to insert): Add a data after the list

Del l[where to delete data]: Delete a data

L.remove (1): Remove one data, no return value

L[4]: Modify the list corresponding subscript data

b = L.pop (): pops up the last data in the list

Len (L): Statistics list length

L.sort (): List sort

L.count (1): Number of occurrences of an element

? Yuan zu

Ganso defined parentheses are not the same as the list

Yongzu's data can't be changed.

In addition to the above two points, the other way to use the same as list

? dictionary

The dictionary consists of key and value

Each key corresponds to a value, which means that the dictionary consists of a pair of key and value

Dictionaries are unordered.

Dictionary name ["Value name"]: Output key is the value of input key

Output full dictionary tiny+ dictionary name

Output all keys: tiny+ dictionary name. Keys ()

Output all values: tiny+ dictionary name. VALUES ()

Python Basic Learning Path no.2 data type

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.