1. Enter two numbers from the keyboard and calculate the power of B for a:
Number1=raw_input (' Input number1: ')
Number2=raw_input (' Input number2: ')
print ' number1 * * number2 is:%d '% (int (number1) * * INT (NUMBER2))
2. Format Specifier%d,%s,%f
I=int (Raw_input ("Please enter monthly profit (million):"))
if (I <= 10):
Reward= i* 0.1
Elif (I>10 and i<=20):
reward=10* 0.1+ (i-10) * (7.5 *0.01)
Elif (I>20 and i<=40):
reward=10* 0.1 + (20-10) *7.5 *0.01+ (i-20) * (5 *0.01)
Else
reward=10* 0.1+10*7.5*0.01+20*5*0.01+ (i-40) * (1 *0.01)
Print "Should put bonus:%.3f"% (reward)
3. Redirect to standard output:
Import Sys
Print >> Sys.stderr, "in Fact,i ' m a Geek"
4. Let Python support Chinese
#coding: Utf-8
5. Use the while loop to iterate through the string:
#coding: Utf-8
Str=raw_input ("Input a String:")
A=0
print ' While result: '
While A<len (str):
Print Str[a],
A+=1 #注意缩进
6.for Looping through Strings:
Str=raw_input ("Input a String:")
print ' \nfor result: '
For C in STR:
Print C,
print ' \nok '
7.for Loop to print numbers within 0-99:
For I in Range (0,100):
Print I,
Range (1,5,2) represents 1 to 5 (5 not included), and the interval is 2.
8. The Classical rabbit problem
def Rabbit (n):
If N==1 or n==2:
Return 1
else:
Return Rabbit (n-1) +rabbit (n-2)
Print "The first year there are%d pairs of rabbits"%rabbit (1)
Print "%d pairs of rabbits in the second year"%rabbit (2)
Print "The sixth year there are%d pairs of rabbits"%rabbit (6)
9. Open the file and read the data for each row
Filename=raw_input (' Enter file name: ')
Fobj=open (filename, ' R ')
For Eachline in Fobj:
Print Eachline,
Fobj.close ()
10. Generate Random Numbers
Import random #这个是注释, introducing modules
Rnd = Random.randint (1,500) #生成1-random number between 500
11. String manipulation
#encoding =utf-8
A= "123456"
Print A[::-1], #翻转序列
Print A[::2], # Take one operation from the other
Print A[::-2]
12. Read the file
f = open ("C:\\1.txt", "R")
lines = F.readlines () #读取全部内容
For line in lines
Print Line
13. Writing Files
f = open ("C:\\1.txt", "r+") #可读可写模式
F.write ("123") #写入字符串
Python Simple Base Code