Python学习第一周

  • 一、我的第一个程序

 

print("Hello word!")

 

  所以说python是一款非常简洁的语言,不像c,c++等等写一个简单的小程序还要调用一堆库。另外,python 3的版本支持中文编写。

 

 

  • 二、变量 的使用

          Python是一种动态的,强类型语言

name="fromzore"
print(name)

 

不用定义变量的类型,系统根据你输入的自动给变量定义

name="fromzore"
age=input("age");
pt2="%s你的年龄是%s"%(name,age)
print(pt2)

因为在input中,他默认你传入的是str类型,所以想用%d就需要强制转换

name="fromzore"
age=int(input("age"))
pt2="%s你的年龄是%d"%(name,age)
print(pt2)

无报错。

  • 三、循环

循环分为while循环和for循环,和c等语言的不同之处在于python的这些循环可以后接else语句

age=18
sum=0
while sum<3:
   count= int(input("age"))
   sum+=1
   if sum==count:
       print("you are right")
       break
   elif age<count:
       print("It is biger than age")
   elif age>count:
       print("It is smaller than age")
else:
    print("Multiple input errors")

结果是这样的

for循环

for i in range(3):
   count= int(input("age"))
   sum+=1
   if sum==count:
       print("you are right")
       break
   elif age<count:
       print("It is biger than age")
   elif age>count:
       print("It is smaller than age")
else:
    print("Multiple input errors")

将while改为for结果是一样的。

ps:所用python版本为python 3。初次发博客有错误或是哪里写的不好,请多多指教。

 

posted @ 2017-11-02 14:45  fromzore  阅读(220)  评论(0编辑  收藏  举报