Python journey 1.3 (first knowledge of python), python journey 1.3 first Knowledge

Source: Internet
Author: User

Python journey 1.3 (first knowledge of python), python journey 1.3 first Knowledge

1. dictionary.

A dictionary is a ing data type in python. It consists of key-value pairs. Almost all types of python objects can be used as keys. However, it is generally the most common to use numbers or strings. The value can be any type of python object, and the dictionary elements are wrapped in.

(1) Pay attention to the related concepts of "key-value pairs.

Assume that the dictionary isDics = {0: 'A', 1: 'B', 'C': 3}

[Explanation] When the key 'key' (key) does not exist, print 'not found' (that is, the information to be processed) and when the key (key) exists, it is the output key value.

(2) Select a value from the dictionary and delete it if it is found. If the key does not exist, you do not want to handle the exception.

[Method] dics. pop ('key', 'not found ')

(3) Add an entry to the dictionary. If no value exists, specify a specific value. If yes, forget it.

[Method] dic. setdefault (key, default)

2. Align code blocks and indentation.

The code block uses indentation alignment to express the code logic, rather than braces. With no additional characters, the program is more readable.

Python has two major features: (1) conciseness. (2) good readability.

3. if statement.

(1) method: if condition:

Statement to be executed after the condition is passed

Elif condition:

Statement to be executed after the condition is passed

Else:

Others

Note: The if statement body is executed only when the value of the conditional expression is true, that is, the statement body is not zero. Otherwise, the program will directly skip this statement body, execute the statement that follows the body of the statement. Here, the statement body can contain both multiple statements and only one statement. However, when the statement body is composed of multiple statements, it must have a uniform indent form, otherwise, a logical error occurs, that is, the syntax check is correct, but the result is not expected.

(2) Now we use an example program to demonstrate the if statement usage. Our program is very simple, as long as the user inputs an integer, if this number is greater than 8, then the output is "too large"; if it is smaller than 8, the output is too small, the Code is as follows:

M = int (input ("please input your number :"))
If (m = 8 ):
Print ("bingo ")
Elif (m> 8 ):
Print ("too large ")
Else:
Print ("too small ")

When we run this program in IDEL, the results are as follows:

 

please input your number:8bingo

 3. while loop.

(1) method:

While expression:

Loop body

(2) The Code displays the application of the while statement.

The following code is used to output 0 ~ Ten:

n=0
while(n<10):
print(“the count is:"n)
n+=1
print("good bye")

 

4. for Loop and range () built-in functions.

(1) The for loop in python is a little different from the traditional for loop. The for loop in python accepts the iteratable object as its parameter, one element of each iteration.

(2) The for statement is a loop control statement in python. It can be used to traverse an object and has an optional else block that is used to process the break statements contained in the for statement.

If the for loop is not terminated by break, the statement in the else block is executed.

Break terminates the for loop when needed

Continue skips the statement behind it and starts the next loop.

(3) The format of the for statement is used as follows:

>>> For <> in <object set>:

... If <condition>:

... Break

... If <condition>:

... Continue

... <Other statements>

... Else:

... <>

...

(4) two small codes are displayed:

For I in 'python': print ("the current letter is:", I)
---------------------------

The current letter is p.
The current letter is y.
Current letter: t
The current letter is: h
The current letter is: o
The current letter is: n

 

Fruits = ["banana", "apple", "watermelon"] for fruit in fruits: print ("fruit:", fruit)
-------------------------------------

The fruit is banana.
Fruit: apple
The fruit is watermelon.

 

(5) another way to execute loop traversal is through indexing, as shown below:

Fruits = ['bana', 'apple', 'mango '] for index in range (len (fruits): print ("current fruit:", fruits [index])
--------------------------------------

Current fruit: banana
Current fruit: apple
Current fruit: mango

 

NOTE: For the above instances, we use the built-in functions len () and range (). The function len () returns the length of the list, that is, the number of elements. Range returns the number of sequences.

(6) use the else statement cyclically. in python,... Else indicates this. The statements in for are no different from those in general. The statements in else are executed after the loop is executed normally (that is, the for statements are not interrupted by the break jump, while... The same is true for else.

Note: The range () function and len () function are used together for string index. There are two ways to traverse the loop (1) through the loop element. (2) Through the index of cyclic elements.

For num in range (): # number of iterations between 10 and 20 for I in range (2, num): # if num % I = 0 according to the factor iteration: # determine the first factor j = num/I # Calculate the second factor print '% d equals % d * % d' % (num, I, j) break # Jump out of the current loop else: # print num of the loop else, 'is a prime number'
--------------------------------------------
10 equals 2*511 is a prime number 12 equals 2*613 is a prime number 14 equals 2*715 equals 3*516 equals 2*817 is a prime number 18 equals 2*919 is a prime number

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.