Python learning day 2, python Day 2

Source: Internet
Author: User

Python learning day 2, python Day 2

The second day of python learning is a disaster. This day was hit, and my own homework was denied. If I did not say it, it is still a good habit to write a blog. Stick to it, I don't know when to stick to it. Haha !!!

The knowledge taught on this day is not much different from that on the first day. The difference is that it is more detailed than that on the first day (we taught by two teachers and the style is different ), this time, I will also write more detailed knowledge points.

Introduction to python

(1) Your program must have a master file.

(2) For python, everything is an object, and objects are created based on classes. # It seems to be understandable, but it seems to be understandable.

Knowledge Point

#__ Divmod _ prints the quotient and remainder of the two numbers in the form of tuples.

Example:

>>> Age = 13 >>> o = age. _ divmod _ (7) >>> print o output result: (1, 6) # rdivmod Division and Division position> age = 13> o = age. _ rdivmod _ (7)> print o output result: (0, 7) #__ abs _ absolute value >>>> age =-13 >>> o = age. _ abs _ () >>> print o13 @ method 2 >>>> abs (-19) 19 #__ add _ add two numbers >>> age =-13 >>> o = age. _ add _ (34) >>> print o21 method 2 >>>>-13 + 3421 string str # type can know which classes the object belongs

>>> Age = 18
>>> Type (age)
<Type 'int'>

# Dir provides object functions

>>> Age = 18

>>> Print (dir (age) ['_ abs _', '_ add _', '_ and __', '_ class _', '_ cmp _', '_ coerce __'... #__ Contains _ whether to contain characters >>> name = 'zzzn' >>> f = name. _ contains _ ('zzn ')> print fTrue method 2> name = 'zzn'> 'zzn 'in nameTrue #. casefold converts uppercase letters to lowercase name = 'zzzn' f = name. casefold () print (f) zzn # add content name = 'zzn 'f = name to both sides of the center. center (20, '*') print (f) output result: * ******** Zzn ********* # conunt statistics have several identical characters: name = 'zzndsfsdfsdsdfsdf 'f = name. count ('Z') print (f) output result: 2 # encode conversion character set conversion binary name = 'zhang zinan 'f = name. encode ('gbk') print (f) output result: B '\ xd5 \ xc5 \ xd6 \ xbe \ xe9 \ xaa' # endswith returns Truename = 'Alex 'f = name. endswith ('E',) print (f) output result: True import module

Brief Introduction

#! /Usr/bin/env python

Import sysprint (sys. argv) # print (sys. version) # output Python version output: ['C:/Users/wodehuipu/PycharmProjects/untitled/day2/dict. py '] 3.5.1 (v3.5.1: 37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] dictionary # dictionary Syntax: >>> dict = {'name ': 'zzzn', 'age': '28', 'job': 'it', 'sex': 'O' }>>> dict {'job ': 'it', 'age': '28', 'name': 'zzn ', 'sex': 'O'} # Add key: values >>> dict ['city'] = 'bj '>>> dict {'job': 'it', 'city': 'bj', 'age ': '28', 'name': 'zxn ', 'sex': 'O'} # view name >>> dict ['name'] 'zzn' # view keydict. keys () # modify the dictionary; change zzn of name to zxn >>> dict ['name'] = 'zxn '>>> dict {'job': 'it ', 'age': '28', 'name': 'zxn ', 'sex': 'O'} # Delete city >>> dict. pop ('city') 'bj '>>> dict {'job': 'it', 'age': '28', 'name': 'zxn ', 'sex': 'O'} # Another deletion method >>> del dict ['sex'] >>> dict {'job': 'it ', 'age': '28', 'name': 'zxn '} # randomly delete a group of data >>> dict {'job': 'it', 'age ': '25', 'name': 'zzzn'} >>> dict. popitem () ('job', 'it') >>> dict {'age': '25', 'name ': 'zzzn'} # convert dict into a list for display >>> dict. items () [('age', '25'), ('name', 'zzn ')] # Find the key. If the key exists, return values, NULL (none)> dict. get ('age') '25' >>> dict. get ('ss') >>> # add multiple values. If you want to modify all the dictionary values without indexing, >>> dict {'job': 'it ', 'age': '28', 'name': 'zxn '} >>> dict ['name'] = 'zzzn', 'zyr ', 'zzr'> dict {'job': 'it', 'age': '28', 'name': ('zzn', 'zyr ', 'zzr')} # Use has_key. If the dictionary contains this key, True is returned. Otherwise, False is returned. >>> dict {'job': 'it', 'age ': '28', 'name': 'zxn '} >>> dict. has_key ('age') True >>> dict. has_key ('sex') False # Clear dict >>> dict. clear () >>> dict {}# assign the value in the list to a values OK value specified for each list value, if not specified, it is null >>> list = ['A', 'B', 'C'] >>> list ['A', 'B ', 'C'] >>> dict. fromkeys (list, 'OK') {'A': 'OK', 'C': 'OK', 'B': 'OK'} >>> dict. fromkeys (list) {'A': None, 'C': None, 'B': None >>> key = dict. fromkeys (list, 'yes') >>> key {'A': 'yes', 'C': 'yes', 'B ': 'Yes'} # Find a key record. If this key exists, return values. Otherwise, create a >>> dict {'A': 'yes', 'C ': 'Yes', 'B': 'yes'} >>> dict. setdefault ('F', 'no') 'no'> dict {'A': 'yes', 'C': 'yes', 'B ': 'Yes', 'F': 'no'} >>> dict. setdefault ('B', 'no') # I have the key 'B', so it will return the values 'yes' # create a new dictionary, if the same key is updated, the original key is overwritten >>> dict # The original dictionary {'A': 'yes', 'C': 'yes ', 'B': 'yes', 'F': 'no'} >>> dcit2 = {'A': 'hao', 'D': 'le ', 'W': 'yu '} # new dictionary created >>> dcit2 {'A': 'hao', 'D': 'le', 'w ': 'yu '}>>> dict. update (dcit2) >>> dict {'A': 'hao', 'C': 'yes', 'B': 'yes', 'D ': 'le', 'w': 'yu ', 'F ': 'No'} # The values of a in the original dictionary is replaced by the values of a in the new dictionary> contacts. items () [('123', ['zzn ', 'xed', 'ssi']), ('123', ['zxn ', 'desg ', 'iuy']), ('000000', ['die', 'kxis ', 'oen'])] contacts = {'000000': ['zzn ', 'it', 'sa '], '000000': ['jack', 'hr', 'hr '], '000000': ['davi', 'cto ', 'cto']} # for I in contacts: # print I, contacts [I] for k, v in contacts. items (): print k, v [root @ test day2] # python zidian. py 111 ['zzzn', 'it', 'sa'] 113 ['davi', 'cto', 'cto'] 112 ['jack', 'hr ', 'hr'] # traverse the dictionary dict = {'name': 'zzn ', 'age': '26', 'job': 'it'} for item in dict: print (item, dict [item]) # Two traversal methods: for k, v in dict. items (): print (k, v) Output: age 26 name zznjob ITage 26 name zznjob IT ####

>>> Dicr = {'job': 'it', 'name': 'zzn ', 'age': 12}
>>> Dicr. keys () # view key
['Age', 'job', 'name']
>>> Dicr. values () # view Elements
[12, 'it', 'zzzn']
>>> Dicr. items () # view dictionaries
[('Age', 12), ('job', 'it'), ('name', 'zzn ')]

These are the newly added content. The job is as follows:

1. Blog 2. Shopping Cart product display, price; purchase, add to shopping cart; payment, if the money is not enough, it is the content of the next day for python to learn. This day is really depressing !!!!

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.