Python 2.7 Getting Started notes

Source: Internet
Author: User
Tags integer division

As a front-end little white, although now able to understand a little Django code, but also to achieve a simple small demand. But I feel that I still have a lot of not understand to feed. For example, __GT, this is greater than the wording of my circle ... So, follow the IMOOC, start with the Python basics ~

Getting started with Python

Data type:

    1. Integer
    2. Floating point number
    3. string --the string is ‘‘ "" any text that is or is enclosed, such as ' abc ', ' XYZ ', and so on.
    4. Boolean value
    5. NULL -The 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.

PRINT Statement : Outputs the specified text to the screen

Note : A python comment begins with the  #  text, followed by a comment until the end of the line.

Variables: In a python program, variables are represented by a variable name, and the variable name must be a combination of uppercase and lowercase English, numeric, and underscore (_) and cannot begin with a number. (This is the same as any other language)

The type of variable itself is called Dynamic language , and it corresponds to static language . Static languages must specify the variable type when defining the variable, and if the type does not match, an error is given. For example, Java is a static language.

define a string: The Python string \ is escaped with.

raw strings and multi-line strings : If a string contains many characters that need to be escaped, we can prefix the string to  r indicate that it is a raw string, and that the characters do not need to be escaped. However r‘...‘ , the notation cannot represent a multiline string, nor can it represent " a string containing and.

Integers and floating-point numbers: Python supports four blending of integers and floating-point numbers directly, and Python's integer operations are still integers, and the result of floating-point numbers is still floating-point numbers

, but the result of mixed integer and floating-point numbers becomes a floating-point number. The integer division of Python, even if it is not endless, the result is still an integer, the remainder is directly thrown away.

If we want to calculate the exact result of 11/4, the " integer and floating-point mixed operation results are floating-point number" law, the two numbers of one into a floating point and then the operation is no problem: 11.0/4 # ==> 2.75

Boolean type :

When the Python interpreter is doing a Boolean operation, it will not go back and return the result directly as long as it can determine the calculation result in advance.

    1. With: True and False # ==> false
    2. Or: True or False # ==> True
    3. Non: not True # ==> False

In Python, Boolean types can also do and, or, and not with other data types, see the following code:

A = Trueprint A and ' a=t ' or ' a=f '

The result of the calculation is not a Boolean type, but the string ' a=t '.

Because Python regards 0 , and is treated as 空字符串‘‘ None False, other numeric values and non-empty strings are considered true, so: true and ' a=t ' evaluates to ' a=t ', continue to calculate ' a=t ' or ' a=f ' calculation results or ' A=t '.

List data type:

    1. Create a List--python built-in data type is a list: list . A list is an ordered set of elements that can be added and removed at any time. The elements contained in the list do not require that all must be of the same data type. >>> L = [' Michael ', ' True]
    2. access list by index , the index starts at 0.

    3. reverse access list

    4. add new element: append () Always add new elements to the tail of the list.   insert () method, which accepts two parameters, the first parameter is the index number, and the second parameter is the new element to be added:

    5. delete element from list: pop () The method always deletes the last element of the list, and it returns the element, so we print out ' Paul ' after executing l.pop (). If the index value is passed as a parameter in the POP (index) method, the element at the specified index in the list is deleted.  

Tuple data type: An ordered list of Chinese translated as "tuples". Tuple and list are very similar, however, once a tuple is created, it cannot be modified.

    1. creating tuple-- creating a tuple and creating a list is the only difference that is used ( ) instead [ ] .
    2. Create a cell tuple--tuple and list, which can contain 0, one, and any number of elements. ()can represent either a tuple or a parenthesis as a priority for the operation,

      It is precisely because the tuple with the () definition of a single element is ambiguous, so Python specifies that the element tuple should have a comma ",", which avoids ambiguity :

      >>> T = (1,) >>> print T (1,)

      Python also automatically adds a "," when printing cell tuples, in order to tell you more explicitly that this is a tuple.

    3. "Variable" tuple: When a tuple contains a list, by changing the list, you can indirectly change the tuple.

dict data type : establishes a set of keys and a set of value mappings, through key to find Value,dict key is not repeatable.

    1. Create dict--curly braces  {}  indicate that this is a dict, and then follow  key:value, write it out. The
    2. access dict--can simply use the form of  d[key]  to find the corresponding value, which is much like the list, but the list must return the corresponding element using the index. and Dict uses key.

      Note:   Accesses the value of dict via key, and Dict returns the corresponding value as long as the key exists. If key does not exist, it will directly error: Keyerror.

      To avoid keyerror, there are two ways to do this:

      First, determine if key exists, in operator:

      if ' Paul ' in D:PR int d[' paul ' 

      If the ' Paul ' does not exist, the IF statement evaluates to False, and the print d[' Paul ' is not executed naturally, thus avoiding the error. The

      second is a GET method provided using the Dict itself, which returns none when key does not exist:

      >>> Print D.get (' Bart ') 59>>> print d.get (' Paul ') None 
    3. dict features
    • Find Fast. Dict search speed is not without cost,dict The disadvantage is that the memory is large, but also waste a lot of content , the list is just the opposite, the memory is small, but the search speed is slow.
    • unordered. the stored key-value sequence pairs are not sequential.
    • Elements that are key must be immutable , and Python's basic types, such as strings, integers, and floating-point numbers, are immutable and can be used as keys. But the list is mutable and cannot be a key.  
    1. Dict is mutable, meaning that we can add new Key-value to dict at any time.
    2. The for-in statement traverses the Dict.
>>> d = {' Adam ': +, ' Lisa ': $, ' Bart ':}>>> for key in D: ...     Print Key ... Lisaadambart

Set data type: Similar to Dict and list, only care about key, do not care about value. Elements are unordered, non-repeating.

Indentation principle, block of code

If,if-else,if-elif-else

If age >=:    print ' adult ' else:    print ' teenager '
---------------------------
If age >=:    print ' adult ' elif age >= 6:    print ' teenager ' Elif age >= 3:    print ' kid ' else:    print ' Baby


For loop, while loop

L = [' Adam ', ' Lisa ', ' Bart ']for name in L:    print Name

















Python 2.7 Getting Started notes

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.