Python basics and python Overview

Source: Internet
Author: User

Python basics and python Overview

Based on the auto-reply requirements mentioned in the previous article, you can capture packets to obtain the login information that requires post to achieve logon. Then, you need to implement auto-reply through python,

However, python has never been used before. It takes a lot of time to systematically learn a language for a function. Therefore, it is necessary to quickly migrate data based on the basics of other languages.

The following is an overview, a brief introduction to basic usage, and a foundation for the subsequent development.

# Coding = UTF-8 ################ Input and Output ################ output instance print 'hello ', 'World' # input Instance name = raw_input (); print 'hello, ', name #, prompt name = raw_input ('Please enter your name :'); print 'hello', name # format conversion. If the input letter or other non-numeric characters, the error birth = int (raw_input ('birth: ') will be reported :')) ################ character representation ################ escape print '\\\ n \ '# avoid escaping print R' \ n \' # multi-line content representation, use three quotation marks to include print '''line1line2ling2 ''' # Boolean value print 3> 2 # Unicode representation of the string with u'', converted to UTF-8 encoding prin T u 'abc'. encode ('utf-8') print u 'Chinese'. encode ('utf-8') # text file encoding #! /Usr/bin/env python #-*-coding: UTF-8-*-################ format the output instance print 'hello, % s '% 'World' # format the integer and decimal number print' % 2d-% 02d '% (3, 1) print' %. 2f '% 3.1415926 # omnipotent formatting % s, can replace all formatting print' % s-0 % s' % (3, 1) print '% s' % 3.1415926 # For Unicode strings, the usage is the same, but it is best to ensure that the replaced string is also the Unicode string print u'hi, % s' % u'jason0539' # output percent sign %. print 'growth rate with double %: % d % '% 7 ################# list ############### # list, variable ordered list classmates = ['Jack', 'bob', 'tracy '] print classmates # The len function gets its length print len (classmates) # gets an element, use brackets to index print classmates [2] # reciprocal index. You can obtain the print classmates [-1] # append element to the end of the array in reverse order. append ('Adam ') print classmates # insert to the specified position classmates. insert (0, 'jason0539') print classmates # pop deletes the end element print classmates. pop () # Add the pop parameter to delete the print classmates element in the specified position. pop (0) print classmates # When the element changes, you can directly assign a value to classmates [0] = 'jason0539 'print classmate S # list can be nested. Two-Dimensional indexes are available. s = ['python', 'java', ['asp ', 'jsp'], 'scheme '] print s [2] [1] # Empty list l = [] print len (l) ############### tuple ################## immutable and ordered array # define the tuples classmates = ('Michael ', 'bob', 'tracy ') print classmatesclassmates = () print classmatest = (1,) print t # note that t = (1) cannot be defined, because it does not define tuple, it is the number of 1. This is because parentheses can both represent tuple and parentheses in the mathematical formula, which leads to ambiguity. Therefore, Python stipulates that, in this case, the calculated result is 1 by parentheses. # Variable tuplet = ('A', 'B', ['A', 'B']) print tt [2] [0] = 'X' t [2] [1] = 'y' print t # On the surface, the tuple elements have indeed changed, but what actually changes is not the tuple element, but the list element. The list to which tuple points at the beginning is not changed to another list. Therefore, tuple's so-called "unchanged" means that every element of tuple will always point to it. That is, if you point to 'A', you cannot change it to 'B' or to a list. Instead, you cannot change it to another object, however, the list itself is variable ################### dictionary dict ############# ### dictionary dict: Key-Value Pair Group, the key of dict must be an unchangeable object. D = {'Michael ': 95, 'Bob': 75, 'tracy ': 85} print d ['Michael'] # Put data into dict, in addition to the value specified during initialization, you can also put the key. Before that, d must be declared, otherwise, the error d ['jason '] = 5390 print d # determines whether the key is in the dictionary, case sensitive #1. in judge print 'jason 'in d #2. the get method provided by dict determines that if the key does not exist, None can be returned, or the default valueprint d specified by the user is returned. get ('Thomas ') print d. get ('Thomas ',-1) #3. to delete a key, use the pop (key) method. The corresponding value is also deleted from dict. pop ('jason ') print d ################ set ################# set and dict are similar, is also a set of keys, but does not store values, there are no duplicate keys # To create a set, you need to provide a list as the input set s = set ([1, 2, 3]) print s # duplicate elements are automatically filtered out in set s = set ([, 4]) print s # By add (key) the method can be used to add elements to the set. You can add them again, but it does not work. add (4) print s # Use the remove (key) method to delete element s. remove (4) print s # determine whether an element is in set s = 5 in sprint s # set can be regarded as a set of unordered and non-repeating elements in a mathematical sense. Therefore, the two sets can perform mathematical operations such as intersection and Union operations s1 = set ([1, 2, 3]) s2 = set ([2, 3, 4]) print s1 & s2print s1 | s2


Author: jason0539

Blog: http://blog.csdn.net/jason0539 (reprinted please explain the source)

Scan the QR code to follow my public account and read different articles.


Related Article

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.