python end of statement_前方高能——Python概念总结

Arithmetic Expressions*

addition: + =

outputs the sum of the two input numbers

multiplication: * =

outputs the product of the two input numbers

subtraction: - =

outputs the difference between the two input numbers

division: / =

outputs the result of dividing the first number by the second Note: if both numbers are whole numbers, the result is truncated to just the whole number part.

Variables and Assignment

Names

A in Python can be any sequence of letters, numbers, and underscores (_) that does not start with a number. We usually use all lowercase letters for variable names, but capitalization must match exactly. Here are some valid examples of names in Python (but most of these would not be good choices to actually use in your programs):

my_name

one2one

Dorina

this_is_a_very_long_variable_name

Assignment Statement

An assignment statement assigns a value to a variable:

- =

After the assignment statement, the variable refers to the value of the on the right side of the assignment. An is any Python construct that has a value.

Multiple Assignment

We can put more than one name on the left side of an assignment statement, and a corresponding number of expressions on the right side:

, , ... = , , ...

All of the expressions on the right side are evaluated first. Then, each name on the left side is assigned to reference the value of the corresponding expression on the right side. This is handy for swapping variable values. For example,

s, t = t, s

would swap the values of s and t so after the assignment statement s now refers to the previous value of t, and t refers to the previous value of s.

Note: what is really going on here is a bit different. The multiple values are packed in a tuple (which is similar to the list data type introduced in Unit 3, but an immutable version of a list), and then unpacked into its components when there are multiple names on the left side. This distinction is not important for what we do in CS101, but does become important in some contexts.

Procedures

A procedure takes inputs and produces outputs. It is an abstraction that provides a way to use the same code to operate on different data by passing in that data as its inputs.

Defining a procedure:

def ():

The are the inputs to the procedure. There is one for each input in order, separated by commas. There can be any number of parameters (including none).

To produce outputs:

return , , ...

There can be any number of expressions following the return (including none, in which case the output of the procedure is the special value None).

Using a procedure:

(, , ...)

The number of inputs must match the number of parameters. The value of each input is assigned to the value of each parameter name in order, and then the block is evaluated.

If Statements

The if statement provides a way to control what code executes based on the result of a test expression.

if :

The code in only executes if the has a True value.

Alternate clauses. We can use an else clause in an if statement to provide code that will run when the has a False value.

if :

else:

Logical Operators

The and and or operators behave similarly to logical conjunction (and) and disjunction (or). The important property they have which is different from other operators is that the second operand expression is evaluated only when necessary.

and

If Expression1 has a False value, the result is False and Expression2 is not evaluated (so even if it would produce an error it does not matter). If Expression1 has a True value, the result of the and is the value of Expression2.

or

If Expression1 has a True value, the result is True and Expression2 is not evaluated (so even if it would produce an error it does not matter). If Expression1 has a False value, the result of the or is the value of Expression2'.

Loops

Loops provide a way to evaluate the same block of code an arbitrary number of times.

While Loops

A while loop provides a way to keep executing a block of code as long as a test expression is True.

while :

If the evaluates to False, the while loop is done and execution continues with the following statement. If the evaluates to True, the is executed. Then, the loop repeats, returning to the and continuing to evaluate the as long as the is True.

For Loops

A for loop provides a way to execute a block once for each element of a collection:

for in :

The loop goes through each element of the collection, assigning that element to the and evaluating the . The collection could be a String, in which case the elements are the characters of a string; a List, in which case the elements are the elements of the list; a Dictionary, in which case the elements are the keys in the dictionary; or many other types in Python that represent collections of objects.

Strings

A string is sequence of characters surrounded by quotes. The quotes can be either single or double quotes, but the quotes at both ends of the string must be the same type. Here are some examples of strings in Python:

"silly"

'string'

"I'm a valid string, even with a single quote in the middle!"

String Operations

length: len() =

Outputs the number of characters in

string concatenation: +

=

Outputs the concatenation of the two input strings (pasting the string together with no space between them)

string multiplication: * =

Outputs a string that is copies of the input pasted together

INDEXING STRINGS

The indexing operator provides a way to extract subsequences of characters from a string.

string indexing: [] =

Outputs a single-character string containing the character at position of the input .

Positions in the string are counted starting from 0, so s[1] would output the second character in s. If the is negative, positions are counted from the end of the string: s[-1] is the last character in s.*

string extraction: [:] =

Outputs a string that is the subsequence of the input string starting from position and ending just before position . If is missing, starts from the beginning of the input string; if is missing, goes to the end of the input string.

find The find method provides a way to find sub-sequences of characters in strings.

find: .find() =

Outputs a number giving the position in where first appears. If there is no occurrence of in , outputs -1.

To find later occurrences, we can also pass in a number to find:

find after: .find(, ) =

Outputs a number giving the position in where first appears that is at or after the position give by . If there is no occurrence of in at or after , outputs -1.

CONVERTING BETWEEN NUMBERS AND STRINGS

str: str() =

Outputs a string that represents the input number. For example, str(23) outputs the string '23'.

ord: ord() =

Outputs the number corresponding to the input string.

chr: chr() =

Outputs the one-character string corresponding to the number input. This function is the inverse of ord: chr(ord(a)) = a for any one-character string a.

SPLITTING STRINGS

split: .split() = [, , ... ]

outputs a list of strings that are (roughly) the words contained in the input string. The words are determined by whitespace (either spaces, tabs, or newlines) in the string. (We did not cover this in class, but split can also be used with an optional input that is a list of the separator characters, and a second optional input that controls the maximum number of elements in the output list.)

LOOPING THROUGH STRINGS

A for loop provides a way to execute a block once for each character in a string (just like looping through the elements of a list):

for in :

**

The loop goes through each character of the string in turn, assigning that element to the and evaluating the .

Lists

A list is a mutable collection of objects. The elements in a list can be of any type, including other lists.

Constructing a list. A list is a sequence of zero or more elements, surrounded by square brackets:

[ , , ... ]

Selecting elements: [] =

Outputs the value of the element in at position . Elements are indexed starting from 0.

Selecting sub-sequences: [ : ] =

Outputs a sub-sequence of starting from position , up to (but not including) position .

Update: [] =

Modifies the value of the element in at position to be .

Length: len() =

Outputs the number of (top-level) elements in .

Append: .append()

Mutates by adding to the end of the list.

Concatenation: + = **

Outputs a new list that is the elements of followed by the elements of .

Popping: .pop() =

Mutates by removing its last element. Outputs the value of that element. If there are no elements in , [].pop() produces an error.

Finding: .index() =

Outputs the position of the first occurrence of an element matching in . If is not found in , produces an error.

Membership: in =

Outputs True if occurs in . Otherwise, outputs False.

Non-membership: not in =

Outputs False if occurs in . Otherwise, outputs True.

Loops on Lists

A for loop provides a way to execute a block once for each element of a list:

for in :

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值