Python Learning note "one"

Source: Internet
Author: User

#/usr/bin/env python3# -*- coding: utf-8 -*-#date: 2016-02-23 ' NOTE: Since the Python source code is also a text file, So, when your source code contains Chinese, you need to be sure to save it as UTF-8 encoding when you save it. When the Python interpreter reads the source code, in order for it to be read by UTF-8 encoding, we usually write these two lines at the beginning of the file:!/usr/bin/env python3-*- coding: utf-8 -*- The first line of comments is to tell the LINUX/OS&NBSP;X system that this is a python executable, and the Windows system ignores this comment; the second line of comments is to tell the Python interpreter to read the source code according to UTF-8 encoding, otherwise The Chinese output you write in the source code may be garbled. Affirming that UTF-8 encoding does not mean that your. py file is UTF-8 encoded, and you must make sure that the text editor is using UTF-8&NBSP;WITHOUT&NBSP;BOM encoded "# 1"--------- The use of variable assignment------------#1.1 Direct assignment #name= ' Liuqinghui ' #1 .2  let the user enter the value name=input (' Please enter password: ') # Print (' hello  ',  name) if name ==  ' liuqh123 ':     print (' I come  in  ') else:   print (' i not come in ') ' Code indentation problem: Python uses indentation to organize blocks of code, Be sure to follow the customary conventions and stick to the indentation of 4 spaces. Otherwise the "copy-paste" function fails, this is the most pit daddy. When you refactor the code, the pasted past code must re-check if the indentation is correct or the error:indentationerror: expected an indented block>>>  else:  File  "<stdin> ", line 1    else:       ^syntaxerror:  What if the inside of a string contains both ' and contains '? You can use the escape character \ To identify, for example: ' I\ ' m \ ' ok\ '! ' 2.2  escape character \ Can escape many characters, such as \ n for line break, \ t for tab, character \ itself to escape, so \ \ means the character is 2.3  if there are many characters in the string need to escape, you need to add a lot of \, in order to simplify, Python also allows you to use R ' to indicate that the string inside of ' is not escaped by default, >>> print (R ' \\\t\\ ') \\\t2.4  If there is a lot of line breaks inside the string, it is not good to read in a line, in order to simplify, Python allows the format of "..." to represent multiple lines of content, >>> print ("Line1... line2... line3") line1line2line32.5   python provides an integer representation of the Ord () function to get the character, and the Chr () function converts the encoding to the corresponding character: >>> ord (' A ') 65>>> ord (' Medium ') ) 20013>>> chr ' B ' >>> chr (25991) ' text ' 2.6  in Python, the format used is consistent with the C language and is implemented as a%, for example: >>>  ' hello, %s '  %  ' World ' hello, world ' >>>  ' hi, %s,  you have $%d. '  %  (' Michael ',  1000000) ' HI,&NBSP;MICHAEL,&NBsp;you have $1000000. ' As you may have guessed, the% operator is used to format the string. Inside the string,%s is replaced with a string,%d is replaced with an integer, there are several% placeholder, followed by a number of variables or values, the order to correspond well. If there is only one%, the parentheses can be omitted. If you're not sure what to use,%s always works, it converts any data type to a string, and sometimes the percent inside the string is a normal character. This time, you need to escape, with a percentage of percent, "# 3---------constant------------" "constant is immutable variable, such as the usual mathematical constant π is a constant. In Python, constants are typically represented in all uppercase variable names: But in fact pi is still a variable, and Python does not have any mechanism to guarantee that PI will not be changed, so, using all uppercase variable names to represent constants is only a customary use, if you must change the value of the variable pi, And no one can stop you. "# 4---------null------------' ' null value is a special value in Python, denoted by none. None cannot be understood as 0, because 0 is meaningful, and none is a special null value. "# 5"---------calculation  ------------"in Python, there are two divisions, one division is/:>>> 10 /  33.3333333333333335/Divide the result is a floating-point number, even if two integers are evenly divisible, the result is also a floating-point number: >>> 9 / 33.0 There is a division is//, called the floor in addition, The division of two integers is still an integer: >>> 10 // 33 you are not mistaken, the whole number of floors except//is always an integer, even if not endless. To do the exact division, use/can. Because//division takes only the integer part of the result, Python also provides a remainder operation that can be used to divide the remainder of two integers: >>> 10 % 31 The result is always an integer regardless of whether the integer is//division or the remainder. The result of an integer operation is always accurate. Note: There is no size limit for python integers, and integers for some languages are limited by size depending on their storage length, such as Java's range of 32-bit integers is limited to-2147483648-2147483647. Python does not have a size limit for floating-point numbers, but is represented as an INF directly beyond a certain range(Infinitely Large). "#--------------------use list and tuple------------' python a data type built-in is lists: A list is an ordered set of elements that can be added and removed at any time. >>1.1 Assign to set: 1) classmates = [' Michael ',  ' Bob ',  ' Tracy ']2) append element to end: Classmates.append (' Adam ') 3) Insert the element into the specified position: Classmates.insert (1,  ' Jack ') >>1.2 get the value of the collection 1) classmates2) classmates[?] Note: When the index (? Python will report a indexerror error when out of range, so make sure the index is not out of bounds, and remember that the index of the last element is Len (classmates)  - 1. If you want to take the last element, in addition to calculating the index location, you can also use 1 to index, directly get the last element: >>1.3 Delete the element of the collection 1) Delete the element at the end of the list, with the Pop () method: Classmates.pop () 2) to delete the element at the specified position, use the pop (i) method, where I is the index position: Classmates.pop (1) 3) to replace an element with another element, you can assign a value directly to the corresponding index position: "Another ordered list is called a tuple: tuples. The tuple and list are very similar, but once the tuple is initialized it cannot be modified, such as the names of the classmates ">>> classmates = " Michael,  ' Bob ',   ' Tracy ') Now, classmates this tuple does not change, it does not have append (), insert () method. Other methods of acquiring elements are the same as lists, and you can use classmates[0],classmates[-1] normally, but you cannot assign them to another element. What is the meaning of immutable tuple? Because the tuple is immutable, the code is more secure. If possible, you can use a tuple instead of a list as much as possible. The trap of a tuple: when you define a tuple, the elements of the tuple must be determined when you define it. >>1.4 finally see a "variable" Tuple:>>> t =  (' A ',  ' B ',  [' a ',  ' B ']) >>> t[2][0] =  ' X ' >>>  t[2][1] =  ' y ' >>> t (' A ',  ' B ',  [' X ',  ' Y ']) original int () function to find a string is not a valid number when the error occurs, the program exits. "#-----------------------Cycle-------------------------' 1>names = [' Michael ',  ' Bob ',  ' Tracy ']for name in names:    print (name) 2>sum = 0n =  99while n > 0:    sum = sum + n     n = n - 2print (SUM)


Python Learning note "one"

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.