Python entry basis (b)

First, the basic grammar

print ();: print statement

#: Python comments

EG:   Print ( 'ABC') which is a single line comment #

II. Variables and identifiers

1) variable

Python used variables need not be declared, you can directly assign values ​​to variables

a = 10

 You can not use variables not been assigned
 if a variable is not assigned before, being given NameError: name 'b' is not defined

Python is a dynamically typed language, can be given any type of value as a variable, modify the value of a variable may be any

a = "hello"

2) Identifier

In Python all content will be free to belong to the named identifiers
such as: variable names, function names, class names,
identifiers must follow the specification identifier

1. identifiers may contain letters, numbers, _, but can not be used at the beginning of the digital
  example: A_1 _A1 _1a
 2. identifier can not be Python keywords and reserved words
  are not recommended to use the function name as an identifier in Python, because it causes the function to be covered
 3. naming convention:
    Note that follows one of two naming convention in Python:
     underline nomenclature
     all lowercase letters, use _ split between words
     max_length min_length hello_world xxx_yyy_zzz
     Pascal nomenclature (large hump nomenclature)  
     initials capital, each word begins with the letter capitalized, remaining letters lowercase
      MaxLength MinLength HelloWorld XxxYyyZzz  
     If you use non-compliant identifier, will be thrown SyntaxError: invalid syntax   

3. Numerical

In Python divided into three values: integer, floating point (decimal), the complex
in Python are all integer type int
A = 10
B = 20 is

 the size of the integer in the Python is not limited, and may be an infinite integer
c ** = 999999999999999999999999999999999999999999999 100

If the length is too large number may be used as separators underscore
c = 123_456_789

= 0123 D 10 decimal numbers can not start with 0, other binary integer, as long as some digital printing form is displayed in decimal
 binary 0b beginning
c = 0b10   binary 10
hex 0x beginning
c = 0x10

It may be performed by the digital arithmetic operators, and ensure accurate integer arithmetic
C = -100
C = C +. 3

Float (decimal), the decimal in Python are all of type float
C = 1.23
C = 4.56

When calculating floating point numbers, may give an inaccurate result
c = 0.1 + 0.2 # 0.30000000000000004

4. String

String (str)
string information used to represent a text string data type is the most used in the program
string in quotation marks are required to cause Python

s = 'hello'

 s = abc string must be enclosed in quotation marks, without using a string not
quoted can be double quotes, single quotes may be, but be careful not to use mixed with
S = 'Hello'
S = "Hello"
 S = 'Hello "
quote may not mix SyntaxError: EOL while scanning string literal

 Between the same can not be nested quotes
 s = "Master said:" Learning While Learning, Lehelehe! ""
S = "Master said:" Learning While Learning, Lehelehe '!'

 Long string
 of single and double quotation marks can not span using
s = 'hoe Wo day when afternoon, \
sweat Wo soil, \
who knows the chopping \
A Journey'

 Using triple quotes for a long string '' ' "" "
 triple quotes can be wrapped, and may remain in the string format

s = '' 'hoe Wo day when afternoon,
sweat Wo soil,
who knows the chopping
A Journey' ''

2) format string

--1.

Adding may be performed between the strings
if the addition of the two strings, the two strings are automatically spliced to a
a = 'abc' + 'haha ' + ' ha'

--2,

When creating a string, the string can be specified in the placeholder
 % s represents any character in the string
% f float placeholder
% d placeholder integer
b = 'Hello% s'% ' Monkey'
B = 'hello% s Hello% s'% (' tom ' ,' Monkey ')
B =' Hello% 3.5s'% 'ABCDEFG' Note:% 3.5s string length limit is between 3-5
B = ' % S Hello '% 123.456
B =' Hello% .2f '% 123.456
B =' Hello% D '123.95%
B =' Oh '

--3,

Format string, can create a formatted string by adding a string f before
the formatting can be directly embedded in the string variable
c = f'hello {a} {b } '

print(f'a = {a}')

The Boolean values ​​and null

1) There are two Boolean values True and False
True True False for false representation
A = True
A False =

2) None (null)
None designed to represent the absence of
b = None

6. The arithmetic operators
 + Addition operator (if it is an addition operation between two strings, string operations will be spell)
- subtraction operator
 * multiplication operator (if the string and multiplying numbers, will be on the string copy operation, the string specified number of times)
 / division operator, always returns the operation result when a floating-point type
 // divisible only reserved bits integer calculation, always returns an int
 ** exponentiation calculation, find the value of a power of several
 % modulo sum of two divided by the number of remainder

7. assignment operator
 = variable may be assigned a value to the left of the equal sign on the right side of the equals sign
+ = a + = 5. 5 corresponds + A = A
 - A = - =. 5 corresponds to A = A -. 5
 * = A 5 * = a * = 5 corresponds to a
** ** = a 5 = a ** = 5 corresponds to a
 / = a / a = = 5 corresponds to a / 5
 // // a = 5 corresponds to a = //. 5 A =
 % A% = =. 5 corresponds to a = a% 5

8, relational operators
relational operators used to compare the relationship between two values, always returns a Boolean value
, if the relation holds, returns True, otherwise return False
 > whether the comparison value is greater than the left right value
> left = Comparative if the value is greater than or equal to the right
 <left comparison value is smaller than the right side value
 <= comparison value is less than or equal to the left of the right value
 == compare the values of two objects are equal
 ! = comparison of two whether the value of the object is not equal to
   equal and unequal comparison is the value of an object, rather than id
iS compare whether two objects are the same object, it compares the object id is
whether is not to compare two objects are not the same object, more object id is
Result = 10> 20 is False #
Result = 30> 20 is True #
Result = 30 <20 is False #
Result = 10> = 10 # True

9. Logical Operators

Logical operators mainly used for some logic determines
 not logical negation
   not the right of the symbol values may be non-operational
       for a Boolean NOT operation will be negate operation, becomes True False, False True change
       for non-Boolean value, NOT operation will first convert it to a Boolean value, and then negated
      
 and the logic
   and values may be made to both sides of the operation symbols
    only in both sides of the symbol values are set to True, will return True, False long as there is a returns False
    and False operation is to find the
    Python and the short-circuit operation and, if the first value is False, not look at the second value  
 

or logic or
   or both sides of the symbol values may be ORed
    OR operation as long as one of two values True, it will return True
    or operation is to find a True
    Python or short-circuit operation or, if the first value True, the second value is not to look

10. The conditional operator (ternary operator)
  Syntax: 1 if the statement of the conditional expression 2 else statement
execution process:
   conditional operator, when executed, will first conditional expressions are evaluated determination
      If the determination result is True, the execution 1 statement, and returns the execution result
      if the determination result is False, the statement 2 is executed, and returns the execution result

 

 

 

发布了96 篇原创文章 · 获赞 370 · 访问量 42万+

Guess you like

Origin blog.csdn.net/wk_beicai/article/details/89136495