Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The homework assigned by teacher python is the result of his own practice.

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

I. Review the content

1. Top

Interpreter

Encoding (2.7default ascii,3.6 default utf-8)

2. Print ("hello") 2.7

Print "hello" 3.6

3. The relationship between codes.

Ascii Universal Code (minimum 2 bytes unicode) gbk utf-8

1 byte minimum 2 bytes 2 Chinese 3 bytes

4. Naming

The initials are not numbers.

Variable name cannot be a keyword

Numeric letter underscore

The meaning of the existence of variables-- > easy to call

5. Conditions

If condition:

Pass

Elif condition:

Pass

Else:

Pass

Pay attention to indentation, colons

6 、 while

While condition:

Pass every time the condition is judged to be infinitely executed

Continue stops the cycle and starts the cycle again.

Break terminates all loops

7. Operator

* =

+ =

-=

/ =

% = take the remainder

One equal sign is an assignment, and two equal signs are equal to

Num = 13

Zq = num% 2 # remainder

If zq = = 0:

# even number

Else:

# Odd

In determines whether the element is in the list or not

Num = "zq"

Li = ["zq", "zw"]

If num in li:

Print ('zai')

Else:

Print ('buzai')

Add another one to determine whether it starts with z.

If num in li and num.startwith ('z')

# and can add a condition

8. Basic data types

Int, plastic surgery

N = 123

N = int (123) # int class-- init--

S = "123"

M = int (s) # string type conversion shaping

Plastic surgery has a length limit, 32-bit computer plus or minus 2 to the 31st power. (python2.7)

There is no long in python3.6. No matter how long the number is, it is int. 2.7 beyond the range, it will be converted to growth and plastic surgery.

S = "132sdfg" # string can only be converted if it contains numbers

M = int (s)

Str, string

S = "zq"

S = str ("zq")

A = 123

M = str (a) converts a number to a string

=

Bytes = "byte type

Str = "string

Purpose: convert bytes to strings

B = objects of byte type

# m = bytes (b)

M = str (bre encoding = "utf-8")

Whether the first letter is capitalized, the space is changed, the case is replaced by a number, and the beginning and end of the letter is looked up.

Number format coding and decoding center left floating right floating connection

Li = ["zq", "ss"]

L1 = "_" .join (li)

L1 corresponding value zq_ss

List, list

I = [11, 22, 22, 33]

I = list (11, 22, 33) this is wrong.

I = list ([111422 list 33]) is a class name followed by only one element in parentheses, but it must be followed by a sequence in square brackets.

=

T = (11, 22, 22, 33)

I = list ([t]) this is a mistake, and the result is like this [(11pd22pj33)]

I = list (t) this is correct

T = [11pr 22pr 33]

T = [11pr 22pr 33,] these two kinds are the same.

Common functions of the list:

Indexes

Slice

For

Length

Enumerate

Delete del li [0]

Del li [0:2]

Unique features:

Flip

Sort

Additional

insert

Index position

Delete

Pop

Expansion

clear

Tuple:

T = (11, 22, 33, 44)

T = tuple (iterable object)

Li = (11, 22, 33, 44)

L1 = tuple (li)

Public functions:

Indexes

Slice

For

Length

Enumerate

In

Unique features:

Number

Index position

Property: cannot be modified.

Dict:

D = {"k": 123, "K2": 65}

D2 = {

"k": 123

"K2": 156

}

Li = [1, 2, 3, 4]

Dictionary: key:10 addition

Value: elements of a list

Dic = []

For iMagazine j in enumerate (li,10):

New_dict = dict (enumerate (li,10))

Public functions:

Indexes

Add dic [key] = vlue

Delete

For

Length

In

Unique features:

Key value get haskey-- "is

Update

Fromkey

String, byte

Str

Bytes

A = "Zhang Qiang" bytes encoded by gbk

B = bytes (a gbk encoding = "gbk") type (b) = > bytes

C = str (bre encoding = "gbk")

Int optimization mechanism

A = 123

B = 123

Id (a)

The same, because of the optimization,-5mm 257

A = 123

B = a

II. Homework

1. Element classification

As shown in the following set of values [11, 22, 33, 44, 55, 66, 77, 88 and 99], all values greater than 66 are saved to the first key of the dictionary, and values less than 66 are saved to the value of the second key.

Both: {K1: all values greater than 66, K2: all values less than 66}

The first method

# li = [11, 22, 33, 44, 55, 6, 7, 88, 99, 90]

# Dictionary

# {

# "K1": [11, 22, 33, 44, 55]

# "K2": [66, 77, 88, 99]

#}

# code

Li = [11, 22, 33, 44, 55, 6, 6, 7, 88, 99, 90]

L1 = []

L2 = []

For i in li:

If i 0 and num allset_all:

Print ('no')

Else:

Print ('OK')

# Total assets

Asset_all = 0

I1 = input ("Please enter total assets:")

Asset_all = int (i1)

Goods = [

{"name": "computer", "price": 1999}

{"name": "mouse", "price": 10}

{"name": "Yacht", "price": 20}

{"name": "Beauty", "price": 998}

]

For i in goods:

# {"name": "computer", "price": 1999}

Print (I ['name'], I [' price'])

Car_dict = {}

# car_dict = {

# "computer": {"price": "Unit Price", "num": 123}

#}

While True:

I2 = input ("Please select the product (Ygamy settlement):") # computer

If i2.lower () = = "y":

Break

# cycle through all the goods and find the ones you need

For item in goods:

If item ['name'] = = i2:

Name = item ['name']

# determine whether the item already exists in the shopping cart, yes, num+1

If name in car_dict.keys ():

# pass

Car_ [name] ['num'] = car_ [name] [' num'] + 1

Else:

Car_ [name] = {"num": 1, "single_price": item ['price']}

Print (car_dict)

# {

# 'computer': {'num': 6,' single_price': 1999} 6 '1999

# 'mouse': {'num': 3,' single_price': 10}} 3'10

#}

All_price = 0

For KJO v in car_dict.items ():

N = v ['single_price']

M = v ['num']

All_sum = massin

All_price = all_price + all_sum

If all_price > asset_all:

Print ('can't afford')

Else:

The effect from the first print to the next frame is completely different.

# Total assets

Asset_all = 0

I1 = input ("Please enter total assets:")

Asset_all = int (i1)

Goods = [

{"name": "computer", "price": 1999}

{"name": "mouse", "price": 10}

{"name": "Yacht", "price": 20}

{"name": "Beauty", "price": 998}

]

For i in goods:

# {"name": "computer", "price": 1999}

Print (I ['name'], I [' price'])

Car_dict = {}

# car_dict = {

# "computer": {"price": "Unit Price", "num": 123}

#}

While True:

I2 = input ("Please select the product (Ygamy settlement):") # computer

If i2.lower () = = "y":

Break

# cycle through all the goods and find the ones you need

For item in goods:

If item ['name'] = = i2:

Name = item ['name']

# determine whether the item already exists in the shopping cart, yes, num+1

If name in car_dict.keys ():

# pass

Car_ [name] ['num'] = car_ [name] [' num'] + 1

Else:

Car_ [name] = {"num": 1, "single_price": item ['price']}

Print (car_dict)

# {

# 'computer': {'num': 6,' single_price': 1999} 6 '1999

# 'mouse': {'num': 3,' single_price': 10}} 3'10

#}

All_price = 0

For KJO v in car_dict.items ():

N = v ['single_price']

M = v ['num']

All_sum = massin

All_price = all_price + all_sum

If all_price > asset_all:

Print ('can't afford')

Else:

Print ('OK')

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report