Knowledge required by Python testers

Source: Internet
Author: User
Tags integer division

Knowledge required by Python testers

A. String definition method

Use single quotes (')

You can use single quotes to indicate strings, just like 'quote me on this. All spaces, that is, spaces and tabs are retained as they are.

Double quotation marks (")

The strings in double quotes are exactly the same as those in single quotes. For example, "What's your name? ".

Use three quotation marks (''' or """)

You can use three quotation marks to indicate a multi-line string. You can use single quotes and double quotes freely in three quotes. For example:

'''This is a multi-line string. This is the first line.This is the second line."What's your name?," I asked.He said "Bond, James Bond."'''

Escape Character

Use \ 'to indicate single quotes -- pay attention to this backslash. Now you can represent the string as 'What \'s your name? '.

The method to indicate this special string is "What's your name? ", That is, double quotation marks or three quotation marks.

In a string, a separate backslash at the end of the line indicates that the string continues in the next line, rather than starting a new line. For example:

"This is the first sentence.\This is the second sentence."

B. Variables

The method for defining variables is similar to that of other languages. variable types are determined by assigning values.

Int type Number = 0 String type String = ''Dict type dict = {}

C. indent

Statements at the same level must have the same indentation. Each group of such statements is called a block

i = 5 print 'Value is', iprint 'I repeat, the value is', i

D. Operators

Operator

Name

Description

Example

+

Add

Add two objects

3 + 5 get 8. 'A' + 'B' get 'AB '.

-

Subtraction

Returns a negative number or a number minus another number.

-5.2 returns a negative number. 50-24 get 26.

*

Multiplication

Returns a string that is repeated several times.

2*3 get 6. 'La '* 3 to get 'lala '.

/

Division

X divided by y

4/3 to get 1 (integer division to get the integer result ). 4.0/3 or 4/3. 0 get 1.3333333333333333

//

Integer Division

Returns the integer part of the operator.

4 // 3.0 get 1.0

%

Modulo

Returns the remainder of the Division.

8% 3 get 2. -25.5% 2.25 get 1.5

<

Less

Returns whether x is less than y. All comparison operators return 1 to indicate true, and 0 to indicate false. This is equivalent to the special variables True and False. Note: These variable names are capitalized.

5 <3 returns 0 (False) and 3 <5 returns 1 (True ). Comparison can be connected at any time: 3 <5 <7 returns True.

>

Greater

Returns whether x is greater than y.

5> 3 return True. If both operands are numbers, they are first converted to a common type. Otherwise, it always returns False.

<=

Less than or equal

Returns whether x is less than or equal to y.

X = 3; y = 6; x <= y returns True.

> =

Greater than or equal

Returns whether x is greater than or equal to y.

X = 4; y = 3; x> = y returns True.

=

Equal

Equal comparison object

X = 2; y = 2; x = y returns True. X = 'str'; y = 'str'; x = y, False is returned. X = 'str'; y = 'str'; x = y returns True.

! =

Not equal

Compare whether two objects are not equal

X = 2; y = 3; x! = Y returns True.

Not

Boolean "Non"

If x is True, False is returned. Returns True if x is False.

X = True; not y returns False.

And

Boolean "and"

If x is False, x and y returns False. Otherwise, it returns the calculated value of y.

X = False; y = True; x and y, returns False because x is False. Python does not calculate y here, because it knows that the value of this expression must be False (because x is False ). This phenomenon is called short circuit computation.

Or

Boolean "or"

If x is True, it returns True; otherwise, it returns the calculated value of y.

X = True; y = False; x or y returns True. Short circuit calculation is also applicable here.

The most common ones are small (equal) at, large (equal) at, (not) equal to, not, and, or

E. Control Flow

If statement
number = 23guess = int(raw_input('Enter an integer : ')) if guess == number:  print 'Congratulations, you guessed it.' # New block starts here  print "(but you do not win any prizes!)" # New block ends hereelif guess < number:  print 'No, it is a little higher than that' # Another block  # You can do whatever you want in a block ...else:  print 'No, it is a little lower than that'  # you must have guess > number to reach here print 'Done'# This last statement is always executed, after the if statement is executed if 'a' in name:  print 'Yes, it contains the string "a"'

The elif and else sections are optional.

While statement
number = 23running = Truewhile running:  guess = int(raw_input('Enter an integer : '))  if guess == number:    print 'Congratulations, you guessed it.'    running = False # this causes the while loop to stop  elif guess < number:    print 'No, it is a little higher than that'  else:    print 'No, it is a little lower than that'else:  print 'The while loop is over.'  # Do anything else you want to do hereprint 'Done'
For statement
For I in range (1, 5): print I is equivalent to for I in range (5): print I

For Loop recursion in this range -- for I in range () is equivalent to for I in [1, 2, 3, 4], this is like assigning each number (or object) in the sequence to I, one at a time, and then executing the program block with each I value

Break statement
while True:  s = raw_input('Enter something : ')  if s == 'quit':    break  print 'Length of the string is', len(s)print 'Done'
Continue statement
while True:  s = raw_input('Enter something : ')  if s == 'quit':    break  if len(s) < 3:    continueprint 'Input is of sufficient length'

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.