Python Learning notes (i)

Source: Internet
Author: User

Syntax Requirements--Indentation unification--constants: Always uppercase--variables: Identifies the first letter must be a letter (uppercase or lowercase), or an underscore; Identifier name other can be letters, numbers, or underscores are case sensitive, uppercase and lowercase are two commonly used words Name variable names, such as name, but sometimes use two words, such as taskdetail, so that the situation is not good to read, so it will be written in the following form, such as: Task_detail,taskdetail,taskdetail. Although the situation is more, but overall to maintain a unified style. Data Type--Dividing numeric types by data characteristics: Boolean ture, False long, standard integer int non-integral floating point float sequence type string str tuple tuple list List image type Dictionary dict collection type mutable set set immutable set Fronzens et--by variability partition hash, immutable data type numeric type immutable collection string tuple variable data type dictionary list mutable collection Data OperationsThe following assumes that the variable A is 10 and the variable B is a 20 arithmetic operation
Operator Describe Instance
+ Add-Two objects added A + B output result 30
- Minus-get negative numbers or one number minus the other -B Output Result-10
* Multiply by two number or return a string that is repeated several times A * b output result 200
/ Except-X divided by Y B/A Output Results 2
% Modulo-Returns the remainder of the division B% A output result 0
** Power-Returns the Y power of X A**b is 10 of 20, output 100000000000000000000
// Divide-Returns the integer part of the quotient 9//2 output result 4, 9.0//2.0 output 4.0
Comparison operation
Operator Describe Instance
== Equals-compares objects for equality (A = = B) returns FALSE.
!= Not equal-compares two objects for unequal (A! = B) returns TRUE.
<> Not equal-compares two objects for unequal (a <> B) returns True. This operator is similar to! =.
> Greater than-returns whether x is greater than Y (A > B) returns FALSE.
< Less-Returns whether x is less than Y. All comparison operators return 1 for true, and return 0 for false. This distinction is equivalent to the special variable true and false. Note that these variable names are capitalized. (A < B) returns TRUE.
>= Greater than or equal-returns whether X is greater than or equal to Y. (a >= B) returns FALSE.
<= Less than or equal-returns whether X is less than or equal to Y. (a <= B) returns True.
Assignment operations
Operator Describe Instance
= Simple assignment operator c = A + B assigns the result of the operation of A + B to c
+= Addition assignment operator c + = A is equivalent to C = C + A
-= Subtraction assignment operator C-= A is equivalent to C = c-a
*= Multiplication assignment operator C *= A is equivalent to C = c * A
/= Division assignment operator C/= A is equivalent to C = c/a
%= Modulo assignment operator C%= A is equivalent to C = c% A
**= Power assignment operator C **= A is equivalent to C = c * * A
//= Take the divisible assignment operator C//= A is equivalent to C = c//A
Bit arithmetic
Operator Describe Instance
& Bitwise-AND operator (A & B) Output result 12, binary interpretation: 0000 1100
| Bitwise OR operator (A | b) output result 61, Binary interpretation: 0011 1101
^ Bitwise XOR OR operator (a ^ b) output result 49, binary interpretation: 0011 0001
~ Bitwise inverse operator (~a) Output result-61, Binary interpretation: 1100 0011, in the complement form of a signed binary number.
<< Left move operator A << 2 output results 240, binary interpretation: 1111 0000
>> Right Move operator A >> 2 output results 15, binary interpretation: 0000 1111
logical operators operator
Logical Expressions Description Example
and X and Y Boolean "and"-if x is False,x and y returns FALSE, otherwise it returns the computed value of Y. (A and B) returns 20.
Or X or Y Boolean "or"-if X is non-0, it returns the value of x, otherwise it returns the computed value of Y. (A or B) returns 10.
Not Not X Boolean "Non"-returns False if X is True. If X is False, it returns TRUE. Not (A and B) returns False
Member operations
operator Description Example
Inch Returns False if the value found in the specified sequence returns TRUE. x in the y sequence, if X returns true in the Y sequence.
Not in Returns True if no value is found in the specified sequence, otherwise false. X is not in the Y sequence if x does not return true in the Y sequence.
The following table lists all the operators from highest to lowest priority:
operator Description
** Index (highest priority)
~ + - Bitwise flip, unary Plus and minus (the last two methods are called [email protected] and [email protected])
* / % // Multiply, divide, modulo, and divide
+ - Addition subtraction
>> << Shift right, left shift operator
& Bit ' and '
^ | Bitwise operators
<= < > >= Comparison operators
<> = = = equals operator
= %= /= //= -= += *= **= Assignment operators
Is isn't Identity operator
In No in Member operators
Not OR and logical operators
Module ImportImport SYS # imports the entire module, call inside the method need to add the module name, such as Sys.argvimport multiprocessing as multi # module name is very long, you can start an alias, and then import from the SYS import ARGV # import only one of the methods in the module from SYS import * # also imports the entire module, can use the method without the module name directly, but may cause conflict, so it is not recommended to import the module using this method. User InteractionName = input (' Please input your name: ')
Age = input (' Please input your: ')
Job = input (' Please input your job: ')
Print ("' personal information of%s:name:%sage:%sjob:%s
---------------------------
‘‘‘
% (Name,name,age,job)
Input is typed as a string to print multiple lines of text to format the integer%f formatted floating-point number with the three-quote '%s ' formatted string%d Process ControlIf ... else ... This process executes only one result, and ends when the first condition is met during execution.        For loop for I in range (1,100) guess age-I in range: ages = input ("Please enter your guess Age:") if-= = 20:print ("correct") Break Else:print ("error") if 9-i >0:print ("You have%d Chances"% (9-i)) Else:pri NT ("Opportunity ran out") while loop while True dead loop

Python Learning notes (i)

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.