Python Basic Data type Introduction

Source: Internet
Author: User
Operator

1. Arithmetic Operation:

2. Comparison operation:

3. Assignment Operation:

4. Logical Operation:

5. Member Arithmetic:

Basic data types

1. Digital

int (integral type)

On a 32-bit machine, the number of integers is 32 bits and the value range is -2**31~2**31-1, which is -2147483648~2147483647
On a 64-bit system, the number of integers is 64 bits and the value range is -2**63~2**63-1, which is -9223372036854775808~9223372036854775807


2. Boolean value

True or False

1 or 0

3. String

"Hello World"

String Common functions:

Remove whitespace

Segmentation

Length

Index

Slice


4. List

To create a list:

Name_list = [' Alex ', ' Seven ', ' Eric ']

Or

Name_list = List ([' Alex ', ' Seven ', ' Eric ')


Basic operation:

Index

Slice

Additional

Delete

Length

Slice

Cycle

Contains


5, Ganso

To create a meta-ancestor:

ages = (11, 22, 33, 44, 55)

Or

ages = tuple ((11, 22, 33, 44, 55))


Basic operation:

Index

Slice

Cycle

Length

Contains


6. Dictionary (unordered)

To create a dictionary:

person = {"Name": "Mr.wu", ' Age ': 18}

Or

Person = dict ({"Name": "Mr.wu", ' Age ': 18})


Common operations:

Index

New

Delete

Key, value, key-value pairs

Cycle

Length


PS: Loops, range,continue and break

Other

1. For loop

The user iterates through the contents of an object in order,

Ps:break, continue

Li = [11,22,33,44]

For item in Li:

Print Item

2, Enumrate

Add an ordinal to an object that can be iterated

Li = [11,22,33]

For k,v in Enumerate (Li, 1):

Print (K,V)

3. Range and Xrange

Specify a range to generate the specified number

Print range (1, 10)

# results: [1, 2, 3, 4, 5, 6, 7, 8, 9]

Print range (1, 10, 2)

# results: [1, 3, 5, 7, 9]

Print range (30, 0,-2)

# results: [30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2]

Exercises

First, the element classification

There is a collection of the following values [11,22,33,44,55,66,77,88,99,90 ...], saving all values greater than 66 to the first key in the dictionary, and saving the value less than 66 to the value of the second key.
That is: {' K1 ': All values greater than 66, ' K2 ': All values less than 66}

Second, find

Finds the elements in the list, removes the spaces for each element, and finds all elements that begin with a or a and end with C.

Li = ["Alec", "Aric", "Alex", "Tony", "Rain"]

Tu = ("Alec", "Aric", "Alex", "Tony", "Rain")

DiC = {' K1 ': ' Alex ', ' K2 ': ' Aric ', ' K3 ': ' Alex ', ' K4 ': ' Tony '}

Third, the output commodity list, the user enters the serial number, displays the user to select the product

Product Li = ["Mobile phone", "Computer", "mouse pad", ' yacht ')

Four, shopping cart

Functional Requirements:

Require users to enter total assets, for example: 2000

Display the list of items, let the user select the item according to the serial number, add the shopping cart

Purchase, if the total amount of goods is greater than the total assets, indicating that the account balance is insufficient, otherwise, the purchase succeeds.

Add: Can recharge, a product to remove the shopping cart


goods = [

{"Name": "Computer", "Price": 1999},

{"Name": "Mouse", "Price": 10},

{"Name": "Yacht", "Price": 20},

{"Name": "Beauty", "Price": 998},

]

V. User interaction, showing the choice of three-level linkage between provincial and municipal counties

DIC = {

"Hebei": {

"Shijiazhuang": ["Luquan", "Gaocheng", "Yuanshi"],

"Handan": ["Yongnian", "she County", "CI"],

}

"Henan": {

...

}

"Shanxi": {

...

}

}

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.