Basic data types of python

1. Numerical type: int (1), float (1.0, 2e8, 2e-8), tra string ("hello"),long(1111L),bool boolean type (True, ...
1. Numerical type: int (1), float (1.0, 2e8, 2e-8), tra string ("hello"),long(1111L),bool boolean type (True, False), complex complex complex type (1+2j)


2.Operators and Expressions (1)Arithmetic operator:+,-,*,**(Power), /, %, //(int) In [1]: 5/2 Out[1]: 2 In [2]: from __future__ import division In [3]: 5/2 Out[3]: 2.5 (2)Assignment Operators (Between the same types):=, +=(a+=b Namely a=a+b), -=, /=, *=, %= (3)Relational operators: >, >=, <, <=, !=, ==(Is the comparison equal) ##The final result is bool (4)Logical operators:Logic andand, Logical oror, Logical nonnot




Built in method cmp ##Compare size, < is - 1, > is 1, = is 0 In [1]: cmp(1,3) Out[1]: -1 In [2]: cmp(5,3) Out[2]: 1 In [3]: cmp(5,5) Out[3]: 0 //Find help: help(cmp) str(),type(),int(),float(),long(),complex(),bool()(No0All are true) ##Transformation In [4]: int = 2 In [5]: float(int) Out[5]: 2.0 divmod() ##Gain business and surplus In [10]: divmod(5,2) Out[10]: (2, 1) abs() ##Take absolute value In [11]: abs(1.2) Out[11]: 1.2 pow() ##**Power In [12]: pow(2,3) Out[12]: 8 round() ##rounding In [13]: round(1.5) Out[13]: 2.0

Port I / O 1.input Receive numeric type data year = input("Enter judgment year:") print type(year) print ( (year%4==0 and year%100!=0) or year%400==0 ) 2.raw_input Accept string type username = raw_input("user name:") password = raw_input("Password:") print "username:%s password:%s" %(username,password) #print "User name is:",username ##Another printing method #print "password is:", password 3.print (1)print "hello" ##print hello (error is reported. At this time, hello is the variable name, and without "" is the variable) (2)print ''' 1:%s 2:%s '''%(1,2) ##'' has three functions,% s,% D,% F,%. 5D.% 2F,% e (3)print "1:%s 2:%s" %(1,2)

if 1. Format of if statement: if (expression): Statement satisfying expression execution else: Statement not satisfied with expression execution Example 1: age = input("age:") if age > 18: print "adult" else: print "minor" Example 2: year = input("input year:") if (year%4==0 and year%100!=0) or year%400==0: print "leap year" else: print "not leap year" 2.if contains multiple conditional expressions (1)warn = "someone" if warn: print "somebody's here! " else: print "all right! "When warn is empty, everything goes well! (2)warn = "1" disk_uasge = 78 if warn or disk_uasge >80: print "server needs maintenance... " else: print "all right! " Example: #!/usr/bin/env python #coding:utf-8 ''' Input information ''' print ("================================================================ Hostname = raw Ou input ("hostname:") IP = raw_input("IP:") used_year =input("service life:") CPU = raw_input("CPU:") Memory = raw_input("Memory:") Manager? Name = raw? Input ("administrator Name:") if used_year > 10: print "the server is too old! " else: print ''' Host name:% s IP:%s Service life:% s CPU:%s Memory:%s '''%(hostname,IP,used_year,CPU,Memory)

while 1.whlie Syntax of the loop: while expression: //Statement satisfying expression execution else: //Statement that does not satisfy the expression ##What other languages don't have //Infinite loop: (not requiredelse) while True: print "hello" //Example: #!/usr/bin/python #coding:utf-8 ''' 1.User name and password system given 2.When the user logs in, enter the user name and password to determine whether the login is successful 3.The user has three chances to log in. If the user fails to log in more than three times, an error will be reported ''' print ("=======User login======") trycount = 0 while trycount < 3: hostname = raw_input("User name:") password = raw_input("User password:") if hostname == "westos" and password == "redhat": print "Login succeeded!" exit() else: if trycount < 2: print "Login failed!" trycount += 1 else: print "More than three times,No more login!" 2.Keywords in loop statements continue ##Jump out of this loop and continue back to the loop statement to execute the next loop break ##Jump out of the loop, no more loops //Example: #!/usr/bin/env python #coding:utf-8 """ 1.cmd = Display command line prompt, waiting for user input 2.If the command is empty, skip this cycle and continue to accept the user command 3.If the command is quit,Jump out of all loops and end the program 4.If there is a command, print"run %s"%(pwd) """ while True: cmd = raw_input("[root@server~]:") if not cmd: continue elif cmd == "quit": break else: print ("run %s")%(cmd)

for for i in Iteratable objects: state1.... break ##Jump out of for loop continue ##Jump out of this cycle else: state2.... //The first iteratable object: range(start,stop,step) //Example: #!/usr/bin/env python #coding:utf-8 ''' //To write the multiplication table: 1*1=1 1*2=2 2*2=4 ... 1*9=9 ... 9*9=81 ''' for i in range(1,10): for j in range(1,1+i): print "%d*%d=%d"%(j,i,i*j), ##Last, no line break print ##Default line feed, perform line feed once

30 April 2020, 13:11 | Views: 6726

Add new comment

For adding a comment, please log in
or create account

0 comments