Python Learning Note 2

Source: Internet
Author: User

#!/usr/bin/python3#-*- coding:utf-8 -*-id_db = {     3713131131:{       ' name '  :  ' Chenhao ',        ' age ': 22,       ' addr '  :  ' Shandong '     },    33333131131: {          ' name ':  "Chen",         ' age ': 24,          ' addr ':  "Dongbei"     }, #修改和添加字典print (id_db[ 3713131131]) id_db[3713131131][' name ']= "Wang" id_db[3713131131][' QQ ']=2322345del id_db[3713131131][' name '] Print (id_db[3713131131]) print ([Id_db.fromkeys], ' DDD ') print (Id_db.setdefault (3713131131), "haha") # Returns None#for key in id_db:#    print (Key,id_db[key]) 
If a key value does not exist

The Python dictionary contains the following built-in functions:
1, CMP (Dict1, DICT2): compare two dictionary elements.
2. Len (dict): Calculates the number of dictionary elements, that is, the total number of keys.
3, str (dict): output dictionary printable string representation.
4. Type (variable): Returns the type of the input variable and returns the dictionary type if the variable is a dictionary.


python dictionary contains the following built-in methods:
1, Radiansdict.clear (): Delete all elements in the dictionary
2, Radiansdict.copy (): Returns a shallow copy of a dictionary
3, Radiansdict.fromkeys (): Creates a new dictionary with the key of the dictionary in sequence seq, Val is the initial value corresponding to all keys of the dictionary
4, Radiansdict.get (Key, Default=none): Returns the value of the specified key, If the value does not return the default value in the dictionary
5, Radiansdict.has_key (key): Returns False if the key is true in the dictionary dict
6, Radiansdict.items () : Returns a traversed (key, value) array of tuples as a list
7, Radiansdict.keys (): Returns a dictionary of all keys in a list
8, Radiansdict.setdefault (Key, Default=none): and get () Similarly, if the key does not already exist in the dictionary, the key is added and the value is set to default
9, Radiansdict.update (DICT2): Updates the dictionary dict2 key/value pairs to dict
10, Radiansdict.values (): Returns all values in the dictionary as a list

#!/usr/bin/python3#-*- coding:utf-8 -*-#写一个购物小程序 # User starts by entering the Payroll # User starts the program and prints the Product list # allows the user to choose to buy the product # allows users to constantly buy a variety of products # Check whether the balance is sufficient at the time of purchase, if enough direct debit, otherwise insufficient printing balance # allows the user to exit the program voluntarily, print the purchased list on exit # to determine whether the number Gongzi = input ("Please enter the salary: ") if  Gongzi.isdigit ():     gongzi = int (Gongzi) else:    exit (" Please enter the number ") #使用center   input Information Center welcome_msg =  ' welcome to shopping mall '. Center (50, '-') Print (welcome_msg) #商品列表product_list  = [     (' iphone ', 5888),      (' Mac air ', 8888),     (' Mar pro ', 9008),     (' Xiaomi2 ', ,     (' coffic ', '),] #定义一个列表shop_car  = [] #标志位, exiting the while loop exit_flag =  falsewhile exit_flag is not true:    print ("Product list"). Center (+, '-'))     for item in enumerate (product_list):         index = item[0]        p_name = item[1][0]         p_price = item[1][1]         print (Index,p_name,p_price)     user_choice = input ("[Q= exit, c= selection] Please select Product  : ")     if user_choice.isdigit (): #肯定是选择商品          user_choice = int (User_choice)         if  User_choice <  len (product_list):             p_item = product_list[user_choice]             if p_item[1] <= gongzi:    #买得起                  shop_car.append (P_item)  # Add to Cart &NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;           gongzi -= p_item[1]    #减钱                 print ("Add  [%s]  to your cart, your balance is  \033[31;1m[%s]"  % (P_item,gongzi)              else:                 print ("Your balance is [%s], cannot buy"  %gongzi)     else:         if  user_choice ==  ' Q '  or user_choice  ==  ' Quit ':              print (" The products you purchased are as follows ". Center (+, ' * '))              for  item in shop_car:                  print (item)              print ("END". Center (40, ' * '))              print ("Your balance is \033[31;1m[%s]"  %gongzi)              exit_flag =  true


This article is from "Hao son of ▁ Yun's finger licking" blog, please be sure to keep this source http://chenhao6.blog.51cto.com/6228054/1864291

Python Learning Note 2

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.