Python Road Day 1

1. Python Variable Name Rules

Must start with a letter or underscore_

Must consist of letters and numbers and underscores

Case sensitive

Good: spam  eggs  spam23  _speed

Bad: 23spam  #sign  var.12

Different: spam  Spam  SPAM

Complement: 76trombones is illegal because it begins with a number. more@ is illegal because it contains an illegal character. 


2. You can not use reserved words as variable names/identifies

and  del  from  not  while

as  elif  global  or  with 

assert  else  if  pass  yield  

break  except  import  print  

class  exec  in  raise  continue  

finally  is  return  def  for  lambda  try  


3. Numeric Expressions

>>>print 4**3

64


4. Operator Precedence Rules

Parenthesis are always respected

Exponentiation(raise to a power)

Multiplication, Division, and Remainder

Addition and Subtraction

Left to right

for example: 1+2**3/4*5=1+8/4*5=1+2*5=1+11=11

Complement: I. Parentheses have the highest precedence and can be used to force an expression to evaluate in the order you want. Since expressions in parentheses are evaluated first, 2 * (3-1) is 4, and (1+1) ** (5-2) is 8. You can also use parentheses to make an expression easier to read, as in (minute * 100) / 60, even it doesn't change the result.

II. Exponentiation has the next highest precedence, so 2**1+1 is 3, not 4, and 3*1**3 is 3, not 27.

III. Multiplication and Division have the same precedence, which is higher than Addition and Subtraction, which also have the same precedence. So 2*3-1 is 5, not 4, and 6+4/2 is 8, not 5.

III. Operators with the same precedence are evaluated from left to right. So the expression 5-3-1 is 1, not 3, because the 5-3 happens first and the 1 is subtracted from 2.


5. Mixing Integer and Floating

>>>print 99/100

0

>>>print 99/100.0

0.99

>>>print 99.0/100

0.99

>>>print 1+2*3/4.0-5

-2.5

Complement: when both of the operands are integers, the result is also an integer; if either of the operands is a floating-point number, Python performs floating-point division, and the result is a float.


6. Types

>>>ddd=1 + 4

>>>print ddd

5

>>>eee='hello ' + 'there'

>>>print eee

hello there

>>>print float(99) / 100

0.99


7. Converting User Input

inp = raw_input('Europe floor?')

usf = int(inp) + 1                                output: Europe floor? 0

print "US floor", usf                                         US floor 1                  


Exercises: 

Exercise 2.2 Write a program that uses raw_input to prompt a user for their name and then welcomes them.

Enter you name: David 

Hello David

code: input = raw_input("Enter your name: ")

           name = str (input)

           print "Hello " + input


Exercise 2.3 Write a program to prompt the user for hours and rate per hour to compute gross pay.

Enter Hours: 35

Enter Rate: 2.75

Pay: 96.24

code: input = raw_input("Enter Hours: ")
           hours = float(input)
           input = raw_input("Enter Rate: ")
           rate = float(input)
           Pay = hours * rate 
           print Pay


Exercise 3.1 Rewrite your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours.

Enter Hours: 45

Enter Rate: 10

Pay: 475.0

code: input = raw_input("Enter Hours: ")
           hours = float(input)
           input = raw_input("Enter Rate: ")
           rate = float(input)
           #try : 
           if hours <= 40:
            Pay = hours * rate
           #print Pay
           else:
            Pay = 40 * rate + (hours - 40) * 1.5 * rate 
           print Pay 


Exercise 3.2 Rewrite your pay program using try and except so that your program handles on-numeric input gracefully by printing a message and exiting the program. The following shows two executions of the program:

Enter Hours: 20

Enter Rate: nine

Error, please enter numeric input

Enter Hours: forty 

Error, please enter numeric input

code: try :
            input = raw_input("Enter Hours: ")
    hours = float (input)
  input = raw_input("Enter Rate: ")
    rate = float (input)
   except :
    print "Error, please enter numeric input"
    quit()
    if hours <= 40:
    Pay = hours * rate
            else:
    Pay = 40 * rate + (hours - 40) * 1.5 * rate 
       print Pay 


Exercise 3.3 Write a program to prompt for a score between 0.0 and 1.0. If the score is out of range, print an error message. If the score is between 0.0 and 1.0, print a grade using the following table: 

Score         Grade

>= 0.9               A

>= 0.8               B

>=0.7                C

>= 0.6               D

< 0.6                 F

code: try :
input = raw_input("Enter score: ")
Score = float(input)
except :
print "Bad score"
quit()


if Score >=0.9 and Score <=1:
print "A"
elif Score >=0.8 and Score <0.9:
print "B"
elif Score >=0.7 and Score <0.8:
print "C"
elif Score >=0.6 and Score <0.7:
print "D"
elif Score <0.6 and Score>=0:
print "F"
else :
print "Bad score"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值