Read "Thinking about Python like a computer scientist"-Notes

Source: Internet
Author: User
Tags logical operators

Chapter II variables, expressions, and statements

1. The basic data type of the variable: Python has integer, Long integer, floating point, string, plural form 5.

2. Using type (), you can view the data type, such as: Type (2), type (' xiaoming '), etc.; type is a function;

3. Numeric operators:

1) addition, subtraction, multiplication, addition, modulus and exponentiation: + 、-、 *,/,% and * *

2) in Python2, division/use of the de-python3 division (integer divided into integers), that is, 2/3 of the result is 0, the divisor result is a floating-point number type, 2/3 =0.6666666666666 ..., and a new operator//is used to denote the de-division.

3) Sequence of operations: exponentiation > Multiplication > Plus and minus

4. The actions the string can use:

1) Splicing: Use +, such as: ' ADFSA ' + ' adfds ' = ' Adfsaadfds '

2) Repeat operation: use *, such as: ' abc ' * = ' abcabcabc '

5. Comments and line breaks:

# I'm the annotation state right now . Print ' a ' # line break use \ symbol; Print ' asdfggk        sdsfsf'

Chapter III: function calls

1. Built-in type conversion functions in Python

int converts a floating-point number to an integer, which directly points the fractional part, such as int (3.999) = 3; or int (' 2 ') = 2, which converts the string;

The float function converts integers and strings to floating-point numbers: Float (32) =32.0 or float (' 3.14159 ') = 3.14159

The STR function converts the argument to a string;

2, define a function without return value, define the method as follows, Def + function name + () +:, function body to have indentation:

def Hello ():   Print ' hello,world! '  Print ' Hello, yinheyi. '

3. Python provides two ways to import modules:

Method One: Import the module name, such as import math, if you access the function inside the module, you need to specify both the module name and function name, intermediate. Separated

Method Two: From the module name Imort function name, at this time, we can directly use the function name, and do not need to specify the module name; Use the From Module name import * All members of the module are imported;

Pros and Cons: The second way to make the code more concise, but, different modules of the same name between the members, there may be conflicts;

The fourth chapter: Case Study: interface design

1. A development plan:

1) Start by writing small programs without the need for function definitions;

2) Once the program runs successfully, it is encapsulated in a function and named;

3) Generalization of this function, add the appropriate formal parameters;

4) You can use refactoring to improve the chances of a program, for example, if you find a few places in the program that have similar code, you can extract them from the old wood to make a suitable general function.

2. Document string:

Its role: The string at the beginning of the function that is used to interpret it.

def Hello (): """ This is a document string that uses three quotation marks to allow the string to be represented across lines; I'm going to cross the line now . """    Print " hello,world! "

Note: Writing such documents is an important part of the interface design. A well-designed interface should also be very simple to explain clearly;

The fifth chapter conditions and recursion

1. Relational operators:

Not equal to ! =
Greater than >
Less than <
Greater than or equal >=
Less than or equal <=

2. Logical operators:

And and
Or Or
Non - Not

3. Boolean and Boolean expressions:

The expression's value is either a true or false expression, or a Boolean expression. Its value is true or false, and they are not strings.

4. Empty statement: In Python, empty statement use Pass, remember!!!!

5. Conditional Judgment Expression:

1) Only one branch:

if x < y:     print'x is less than y'

2) There are two branches:

if x < y:    print'x is less than y'else:     print'x is large than y'

3) Multiple branches:

if x < y:    print'x is less than y'elif:     print'x is large than y'else:      Print'x is equal to Y'

6. Enter from the keyboard:

Python 2 provides built-in function raw_input to get input from the keyboard, which is changed to input in Python 3, but they use the same.

A = Raw_input ('input your name:\n')print' +a+'! '
# the output is: input your Name:yinheyihello yinheyi!

Sixth: Functions with return values

1. Incremental Development Program:

This idea is really good, we can put a little bit of a large function of the development out, slowly a little bit of debugging. Add some scaffolding code as necessary. For example, print the value of a variable in a critical position. Step by step to ensure that every step is correct, so that we can save a lot of debugging time assist.

2. function with return value: Return value is returned using return.

def sum (A, B):     = A + b    return Temp

3. Adhere to the belief:

When writing recursive functions, we can use a method: stick to the faith. When we encounter a function call, we do not track the execution of the process, but we assume that the function is working correctly and can return the correct result. such as the famous Fibonacci sequence, its program can be written:

def Fibonacci (N):     if n = = 0        :return     0elif n = = 1        :  Return 1    else:    return Fibonacci (n-1) + Fibonacci (n-2)

4. Check the type:

Python has built in a function isinstance to check the type of the arguments, return true correctly, otherwise return False

Isinstance (int) True>>> isinstance (12.2, float) True>>> isinstance ('  Hello', str) True

Chapter Seventh Iteration

1. While loop:

 while n <    :print  n    = n-1

2. For loop:

 for  in range    :print  i    = i + 1

Eighth chapter string sequence

1. The string is a sequence, and its subscript is 0-beginning; Use the Python built-in function Len () to return the number of characters in a string:

' Hello '>>> len (a)5>>> a[0]'h'

You can use a for loop to iterate through a character within a string:

' Hello '  for inch A:     Print Char

2. The string can be sliced:

The operator [n:m] returns the part of the character from the nth character to the first m character, containing the nth character, but excluding the first m characters.

' Banana '>>> fruit[1:3]'an'>>> fruit[:2]'  ba'>>> fruit[3:]'ana'

3. Operator in

In is a Boolean operator that operates on two strings and returns true if the first is a substring of the second, otherwise False.

' Yin ' inch ' yinheyi ' True

4. The elements in the string cannot be changed;

Tenth chapter: List

1. A list is a sequence in which the value can be any type, the value in the list is called an element, and the list can be nested within a list.

The simplest way to create a list is to use [], such as: [Ten, +, +, ' xiaoming ']

2. Use the [] operator to access the list, and the elements in the list can be changed;

3. Iterate through a list, so you can use the for () loop;

 for inch array:     Print I

4. List operations:

1) + operation, can be used for splicing list;

2) * operator, can be used to repeat a list multiple times;

5. Remove the element from the list:

1) Delete using subscript: Pop modifies the list, returns the deleted value, and if no subscript is provided, it deletes and returns the last element;

t = ['a' 'b''c']  Print T.pop (1)# output: bprint  t# output: [' a ', ' C ']

2) Use the subscript to delete, do not return the deleted value, use the DEL operator:

>>> T =[1, 2, 3, 4]del t[1]print  t[1, 3, 4]>>> t =[1, 2, 3, 4]del t[1:3]print  t[1, 4]

3) If you know the deleted element, you can use remove:

>>> T = ['a','b','C','D','e']>>> T.remove ('D')>>>Printt['a','b','C','e']

6. Convert the string to a list:

1) Convert to single word: Use function: List

' Asdfas ' Print list (t) ['a's'd'  'f'a's' ]
2) Convert to a word, use the string method split ()

' Hello World yinheyi ' Print t.split () ['hello'  ' world ' Yinheyi']

7. An interesting example:

>>> A ='AAA'>>> B ='AAA'>>> A isbtrue>>> a = ['a','b','C']>>> B = ['a','b','C']>>> A isBfalse

Why is it like this? It doesn't mean anything. The string is the same pair, and the list is not the same object, it is easy to understand, the string can not be changed, and the list can be changed;

8. Important: Aliases

When we use Assignment B =a, these two variables refer to the same object, changing one of them will inevitably affect the other, which is different from the C language because they are two different objects in C, as in the following example:

>>> a = [111, 222, 333]>>> b = aprint  a[444, 222, 333]

9. List parameters:

When we pass a list as a parameter into a function, the function gets a reference to the list. Therefore, in the function to modify the list, the function is also affected outside;

This chapter gives special attention to the fact that the assignment of a list is a reference to the original list. To change the list or create a new list;

11th Chapter: Dictionary

Read "Thinking about Python like a computer scientist"-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.