python学习之语法

  看过python的语法,才知道,这中编程语言是最简洁的,比如if a==b这种判断a和b的值是否相等,其他编程语言是需要小括号的,这个就不用。

  if语句:

cars = ['audi', 'bmw', 'subaru', 'toyota']
for car in cars:
    if car == 'bmw':
        print(car.upper())
    else:
        print(car.title())

  上述代码中包含了for循环语句一级if语句,要特别注意冒号和缩进

  if-elif-else 结构 :

  在其他类编程语言中,大部分都是if-else if-else结构,而在python中,则是if-elif-else 结构。

age = 12
if age < 4:
    print("Your admission cost is $0.")
elif age < 18:
    print("Your admission cost is $5.")
else:
    print("Your admission cost is $10.")            

  特殊判断元素

  in判断元素是否在列表中。

requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
for requested_topping in requested_toppings:
    print("Adding " + requested_topping + ".")
    print("\nFinished making your pizza!")

  总结:

  对于一个熟知多种编程语言的学生,我们会很快接受这些语法,最终要的就是缩进,缩进,缩进,其他的都或多或少有了些编码的习惯。

  推荐一个网站:https://leetcode.com/problemset/all/ 在编程中熟知语法以及各类函数的使用。

  

posted @ 2017-09-14 15:34  _小学生  阅读(186)  评论(0编辑  收藏  举报