Python data types, operators, statements, loops

Keywords: Programming less calculator

1. Data type

  • Numbers(int,long,float,complex)
  • String
  • Boolean(True,False)
  • List
  • Tuple
  • Dict
  • Set

2. Data Type Conversion

  • int(float, string, bytes)
  • float(int, string, bytes)
  • complex(int, float, str)
  • str(int, float, complex, bytes, list, tuple, dict, set, other types)
  • bytes(string)
  • list(str, bytes, tuple, dict, set)
  • tuple(str, bytes, list, dict, set)
  • dict(str, list, tuple, set)
  • set(str, bytes, list, tuple, dict)

3. operator

  • +,-,*,/,**/<,>,!=,//,%,&,|,^,~,>>,<<,<=,>=,==,not,and,or
#The sum of two numbers
sumNumber=1+2
print(sumNumber)      #Output: 3
 
#Addition of two strings
sumString="Nice work"
print(sumString)      #Output: Nice work
 
#Subtraction of two numbers
subNumber=2-1
print(subNumber)      #Output: 1
 
#Multiplication of two numbers or repetition of strings
multiplicationNumber=2*3
print(multiplicationNumber)      #Output: 6
multiplicationString="hello"*2
print(multiplicationString)      #Output: hellohello
#/ The blog about * repeating strings has already been introduced.-/
 
#Dividing Two Numbers
divisionNumber=9/2
print(divisionNumber)      #Output: 4
divisionNumber=9.0/2
print(divisionNumber)      #Output: 4.5
divisionNumber=9/2.0
print(divisionNumber)      #Output: 4.5
#/ If any of the divisors or dividends is decimal, the quotient will retain the decimal, and vice versa.-/
 
#Exponentiation
powerNumber=2**3 #The third power equivalent to 2 is 2*2*2. You should be familiar with power operation in mathematics.
print powerNumber       #Output: 8
 
#Less than the symbol, the return value is bool
lessThan=1<2
print(lessThan)        #Output: True
lessThan=1<1
print(lessThan)        #Output: False
 
#Greater than the symbol, the return value is bool
moreThan=2>1
print(moreThan)        #Output: True
moreThan=2>2
print(moreThan)        #Output: False
 
#Not equal to the symbol return value being Bool value
notEqual=1!=2
print(notEqual)        #Output: True
notEqual=1!=1
print(notEqual)        #Output: False
 
#The integer part of the division // return quotient, discarding the remainder
divisorNumber=10//3
print(divisorNumber)        #Output: 3
 
#Dividing operation% returns the remainder of quotient, discarding quotient
divisorNumber=10%3
print(divisorNumber)        #Output: 1
divisorNumber=10%1
print(divisorNumber)        #Output: 0/-- Returns 0 if there is no remainder/
divisorNumberx=10//3 # divisorNumberx is the integer part of quotient
divisorNumbery=10%3         #DiisorNumbery is the remainder
divisorNumberz=3*divisorNumberx+divisorNumbery    #Divisor Numberz is the integer part of divisor multiplied by quotient plus remainder, and the value of divisor Numberz is the dividend.
print(divisorNumberz)        #Output: 10
 
#Bit-by-bit and operation & Bit-by-bit refers to the conversion of a number into binary, and then these binary digits are bitwise for operation.
operationNumber=7&18
print operationNumber        #Output: 2
'''
//This is a bit around, a little more, if not too familiar with binary friends, you can turn on the computer's own calculator, hold down win+q, enter "calculator".
//Then the open calculator is set to programmer mode, which is View - > programmer.
//Then we convert 7 to binary: 111, auto-complete 8 bits: 00000111, and then 18 to binary complete 8 bits to get: 00010010.
//Finally, it will be 00000111
//The operation is carried out in bit with 00010010.
/-
//For those who are not familiar with the operation of Baidu Encyclopedia can see the introduction, or very detailed.
http://baike.baidu.com/link?url=lfGJREBvGCY5j7VdF2OO9n2mtIbSyNUD7lZyyY74QIetguL5lXIQUxY38Yr-p4z4WdUvHUKGjw9CDfagiun2Ea
-/
//Result: 00000010
//We all know that 10 binary decimal = 2, so the bitwise sum of 7 and 18 is binary 10 (decimal 2).
'''
 
#Bit-by-bit or operation |, bit-by-bit or referring to the conversion of a number into binary, and then these binary digits are bit-by-bit carried out or operated on.
operationNumber=7|18
print operationNumber        #Output: 23   #The way to solve the problem is the same as that of the bits and operations. You can refer to the bits and operations.
 
#xor
operationNumber=7^18
print operationNumber        #Output: 21   #The way to solve the problem is the same as that of the bits and operations. You can refer to the bits and operations.
 
#Bit-by-bit flip-by-bit flip formula: ~x= - (x+1)
operationNumber=~12  #~12=- (12+1) = -13
print operationNumber        #Output results:-13   #The way to solve the problem is the same as that of the bits and operations. You can refer to the bits and operations.
 
#Left shift
'''
//For example, moving 18 left is to move his binary form 00100100 left and get 00100100 (36).
//The law of left shift: one unit of left shift equals to multiply 2, two units of left shift equals to multiply 4, and three units of left shift equals to multiply 8.
//That is to say, moving n units to the left is equivalent to multiplying the n power of 2.
'''
operationNumber=12<<1
print operationNumber        #Output: 24
operationNumber=3<<3
print operationNumber        #Output: 24
 
#Right shift "
'''
//After understanding the left shift, the right shift is very easy to understand.
//The right shift is an inverse operation of the left shift, which moves the corresponding binary number to the right.
//Law of Right Shift: One unit of Right Shift equals to divide by 2, two units of Right Shift equals to divide by 4, and three units of Right Shift equals to divide by 8.
//That is, to move n units to the right is equivalent to dividing n powers by 2.
'''
operationNumber=12>>1
print operationNumber        #Output: 6
operationNumber=12>>2
print operationNumber        #Output: 3
 
#Less than or equal to <= comparison operation, less than or equal to return a bool value
operationNumber=3<=3
print operationNumber        #Output: True
operationNumber=3<=2
print operationNumber        #Output: False
 
#Greater than or equal to >= comparison operation, greater than or equal to return a bool value
operationNumber=2>=3
print operationNumber        #Output: False
operationNumber=3>=2
print operationNumber        #Output: True
 
#Compare the equality of two objects==
operationNumber=3==2
print operationNumber        #Output: False
operationString="hi"=="hi"
print operationString        #Output: True
 
#Logical non-not
operationx=True
operationy=not operationx
print operationy        #Output: False
operationz=False
print not operationz        #Output: True
 
#Logic and and
'''
True and True = True
True and False = False
False and True = False
False and False = False
'''
print True and True        #Output: True
 
#Logic or or
'''
True or True = True
True or False = True
False or True = True
False or False = False
'''
print False or False        #Output: False

4. Operator priority

Sort from high to low

  • * * index
  • ~ +, -, negative, plus or minus sign
    • ,/,%,//multiply, divide, modulus and divide
  • +,-plus and minus sign
  • Right-shift, left-shift operators
  • Place and place
  • ^| Bit Operator
  • ==,!=,<,<=,>>= comparison operator
  • =,-=,+=,%=,/=,/=,/=,=,=,*= assignment operator
  • Is, is not identifier operator
  • In, not in member operators
  • and, or, not logical operators

4. statement

  • if,if...else...,if...elif...else...
  • switch...case
  • while,while...do,do...while...
  • for

5. String operation

  • find,index,count,replace,split
  • capitalize,title,startsWith,endsWith,lstrip
  • lower,upper,ljust,rjust,center
  • rstrip,strip,rfind,rindex,partition
  • rpartition,splitlines,isalpha,isdigit,isalnum
  • isspace,join

6. Common operations of lists

  • append,extend,insert
  • in,not in,index,count
  • del,pop,remove

7. Dictionary operation

  • len,keys,values,items,has_key

8. method

# Calculate the cumulative sum of 1~num
    def calculateNum(num):
        result = 0
        i = 1
        while i<=num:
            result = result + i
            i+=1
        return result
    result = calculateNum(100)
    print('1~100 Cumulative sum of:%d'%result)

Posted by gchouchou on Tue, 16 Apr 2019 14:18:32 -0700