Python Basics Overview

Source: Internet
Author: User

or based on the requirements of the automatic Huitie mentioned in the previous article, we can get the login information that need post by grasping the packet, realize the login, and then realize the automatic huitie through Python.

But before using Python, for a function to learn a language is bound to waste a lot of time, so it is necessary to quickly, based on other language basis for rapid migration.

The following is a brief summary of basic usage, which is the basis for the next development.

# coding=utf-8############### #输入输出 ################ output instance print ' Hello ', ' World ' # input Instance name = Raw_input ();p rint ' Hello, ', name# input, prompt name=raw_input (' Please enter your name: ');p rint ' Hello ', name# format conversion, if input letters such as non-numeric characters will be error birth = Int (Raw_input (' Birth: ') ############### #字符表示 ############### #转义print ' \\\n\\ ' #防止转义print R ' \\\n\\ ' #多行内容表示, with three quotes including print " Line1line2ling2 ' ' #布尔值print 3>2#unicode the string represented by U ' ', translates to UTF-8 encoded print U ' ABC '. Encode (' utf-8 ') print U ' Chinese '. Encode (' Utf-8 ') #文本文件编码 #!/usr/bin/env python#-*-coding:utf-8-*-############### #格式化 ############### #格式化输出实例print ' hello,%s ' % ' World ' #格式化整数和小数print '%2d-%02d '% (3,1) print '%.2f '%3.1415926# Magnum formatted%s, can replace all formatted print '%s-0%s '% (3,1) print '%s '% 3.1415926# is exactly the same for Unicode strings, but it is best to ensure that the substituted string is also a unicode string print u ' hi,%s '% u ' Jason0539 ' #输出百分号%, with a double% print ' growth rate:% d%% '%7############### #列表list ############### #列表list, variable ordered list classmates = [' Jack ', ' Bob ', ' Tracy ']print classmates# The Len function gets its length print len (classmates) #获取某个元素, with the bracket index print classmates[2] #倒数索引, you can get the array element print in reverse-classmaTes[-1] #append追加元素到末尾classmates. Append (' Adam ') print Classmates#insert inserted into the specified position classmates.insert (0, ' Jason0539 ') Print Classmates#pop Delete End element print Classmates.pop () #pop add parameter delete specified position element print Classmates.pop (0) print classmates# element changed, Direct assignment is classmates[0]= ' Jason0539 ' Print classmates#list can be nested, usable two-dimensional index s = [' Python ', ' Java ', [' ASP ', ' jsp '], ' scheme ']print s [2] [1] #空列表l =[]print len (l) ############### #元组tuple ############### #不可变有序的数组 # define tuple classmates= (' Michael ', ' Bob ', ' Tracy ') Print classmatesclassmates= () print classmatest= (1,) print t# Note that T = (1) cannot be defined because it defines a tuple, which is 1, because the parentheses can represent a tuple, It can also represent the parentheses in the mathematical formula, which creates ambiguity, so Python rules that, in this case, the parentheses are calculated and the result is naturally 1. #表面上可变的tuplet = (' A ', ' B ', [' A ', ' B ']) print tt[2][0]= ' X ' t[2][1]= ' Y ' print t# on the surface, the elements of the tuple do change, but in fact it's not a tuple element, But the elements of the list. The list that the tuple initially points to is not changed to another list, so the so-called "invariant" of a tuple is that each element of a tuple is directed to never change. That is, point to ' a ', cannot be changed to point to ' B ', point to a list, can not be changed to point to other objects, but the list itself is a variable ############### #字典dict ############### #字典 dict is a key value pairs group, The Dict key must be an immutable object. d={' Michael ': Up, ' Bob ': ", ' Tracy ': 85}print d[' Michael ' #把数据放入dict的方法, except initializationWhen specified, it can also be placed via key, before which D must be declared, otherwise it will error d[' Jason ']=5390print d# determine if key is in the dictionary, case-sensitive #1.in judge print ' Jason ' in D#2. Judging by the Get method provided by Dict, if key does not exist, you can return none, or return your own default Valueprint d.get (' Thomas ') Print D.get (' Thomas ', -1) #3. To delete a key, use pop (key) method, the corresponding value is also removed from the Dict d.pop (' Jason ') print d############### #集合set ############### #set和dict类似, which is also a set of keys, But does not store value, there is no duplicate key# to create a set, you need to provide a list as an input set of S = Set ([All-in-one]) print s# repeating elements are automatically filtered in set S = Set ([1,2,3,2,3,2,4]) print s# The add element can be added to the set by adding it, but without effect S.add (4) Print s# removes the element through the Remove (key) method S.remove (4) Print s# determines whether the element is in set S = 5 in Sprint S#set can be seen as a set of unordered and non-repeating elements in mathematical sense, so that two sets can do the intersection of mathematical meanings, and the set of S1=set ([three-way]) S2=set ([2,3,4]) print S1 & s2print S1 | S2


jason0539

Blog: http://blog.csdn.net/jason0539 (reprint please indicate the source)

Sweep code Follow me public number, look at different articles


Python Basics Overview

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.