Python Learning (chapter II)

Source: Internet
Author: User

One, variable

1. Type (variable name) to see the types of the variable

2. For string two operators + and * , respectively, perform stitching and repeat operations

3. Formatted output

%s String
%d Integral type (%06d reserved six-bit front complement 0)
%f Floating-point number (%.2f reserved 2 digits after decimal point)
%% Percent Sign
Name ='Xiao Ming'Print('My name is%s, please take care! '%name) Student_no= 100Print('My school number is%06d.'%student_no) Price= 8.5Weight= 7.5 Money= Price *WeightPrint('Apple Unit Price%.2f yuan/catty, purchased%.3f Jin, need to pay%.4f yuan,'%(Price,weight,money))#defines a decimal scale. The output is the data scale is 10.00%Scale = 0.25Print('data ratio is%.2f%%'% (100*scale))
-----------------------------------------

My name is Xiao Ming, please take care!
My school number is 000100.
Apple Unit Price 8.50 Yuan/catty, purchased 7.500 pounds, need to pay 63.7500 yuan,
Data ratio is 25%

4. Variable name: Letters, numbers, underscore numbers can not start, cannot duplicate the keyword

# View a python keyword list Import keyword Print (keyword.kwlist)
--------------------------------------
[' False ', ' None ', ' True ', ' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' fi  Nally ', ' for ', ' from ', ' global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' nonlocal ', ' not ', ' or ', ' Pass ', ' raise ', ' return ', ' Try ', ' while ', ' with ', ' yield ']

5. Three logical operators: and, OR, no

Second, the processing of random numbers

1. Import Random number Module

Import Random

2. Add one directly behind the module name in the Ipython. Pressing the TAB key will prompt all functions in the package

3. For example

Random.randint (A, B), returns an integer between [a, b], containing a, b

Three, while statement basic usage

1. Specified number of times execution: while + counter (Initialize counter i = 0 between while start)

2. Continue does not perform the section after continue in this cycle, Note: Avoid a dead loop, and then write again in the coninue condition (IF) counter i = i+1

i == 0 while i<5:    if i ==3:        = i+1        continue     Print(i)    = i+1print(' i= at end of Loop') , I)
---------------------------------------------------

0
1
2
4
I= 5 After the end of the cycle

Four, print knowledge points

Print () outputs a newline after the contents of the output are added by default

If you do not want to have a line break, you can add it later, end= ""

 print  ( '   *   " )   Print  ( " *   '    
Print ('*', end='---') Print ('*')
---------------------------------------------------

*
*
**
*---*

Cancel the method of this format and add a print ("")

Print ('*', end=') Print ('#  this time change line plus print("123")
---------------------------------------------------

*
123

Python Learning (chapter II)

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.