编写第一个python程序(Your Firsr Program)

1)代码如下:

# This program says hello and asks for my name.
myName = input("What is your name?")
print(‘it is good to meet you,‘+myName) #ask for your name
print(‘it is good to meet you [%s]‘%myName)

print(‘the length of your name is %s:‘%(len(myName)))
myAge = input("What is your age?") #ask for your age

print(‘You will be ‘ + str(int(myAge)+1) + ‘ in a year.‘)

  输出结果:

What is your name?Sara
it is good to meet you,Sara
it is good to meet you [Sara]
the length of your name is 4:
What is your age?30
You will be 31 in a year.

2)解析程序(Dissecting Your Program)

  • COMMENTS:使用#代表注释,帮助自己知道代码是将要做什么;
  • THE PRINT() FUNCTIONS:打印出括号里面的内容;
  • THE INPUT() FUNCTIONS:等待用户输入,并且按ENTER键;
  • THE LEN() FUNCTION:输出字符串的长度

3)The str(),int()和float()函数

>>>str(0)

‘0‘

>>>str(-3.14)

‘-3.14‘

>>>int(‘42‘)

42

>>>int(‘-99‘)

-99

>>>int(1.99)

1

>>>float(‘3.14‘)

3.14

>>>float(10)

10.0

>>>42 == 42.0

True

>>>42.0 == 0042.00

True

相关推荐