You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

05 Control Flow.py 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # ---
  2. # jupyter:
  3. # jupytext_format_version: '1.2'
  4. # kernelspec:
  5. # display_name: Python 3
  6. # language: python
  7. # name: python3
  8. # language_info:
  9. # codemirror_mode:
  10. # name: ipython
  11. # version: 3
  12. # file_extension: .py
  13. # mimetype: text/x-python
  14. # name: python
  15. # nbconvert_exporter: python
  16. # pygments_lexer: ipython3
  17. # version: 3.5.2
  18. # ---
  19. # All the IPython Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures
  20. # # Control Flow Statements
  21. # ## If
  22. # if some_condition:
  23. #
  24. # algorithm
  25. x = 12
  26. if x >10:
  27. print("Hello")
  28. # ## If-else
  29. # if some_condition:
  30. #
  31. # algorithm
  32. #
  33. # else:
  34. #
  35. # algorithm
  36. x = 12
  37. if x > 10:
  38. print("hello")
  39. else:
  40. print("world")
  41. # ## if-elif
  42. # if some_condition:
  43. #
  44. # algorithm
  45. #
  46. # elif some_condition:
  47. #
  48. # algorithm
  49. #
  50. # else:
  51. #
  52. # algorithm
  53. x = 10
  54. y = 12
  55. if x > y:
  56. print("x>y")
  57. elif x < y:
  58. print("x<y")
  59. else:
  60. print("x=y")
  61. # if statement inside a if statement or if-elif or if-else are called as nested if statements.
  62. x = 10
  63. y = 12
  64. if x > y:
  65. print("x>y")
  66. elif x < y:
  67. print("x<y")
  68. if x==10:
  69. print("x=10")
  70. else:
  71. print("invalid")
  72. else:
  73. print("x=y")
  74. # ## Loops
  75. # ### For
  76. # for variable in something:
  77. #
  78. # algorithm
  79. for i in range(5):
  80. print(i)
  81. # In the above example, i iterates over the 0,1,2,3,4. Every time it takes each value and executes the algorithm inside the loop. It is also possible to iterate over a nested list illustrated below.
  82. list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  83. for list1 in list_of_lists:
  84. print(list1)
  85. # A use case of a nested for loop in this case would be,
  86. list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  87. for list1 in list_of_lists:
  88. for x in list1:
  89. print(x)
  90. # ### While
  91. # while some_condition:
  92. #
  93. # algorithm
  94. # +
  95. i = 1
  96. while i < 3:
  97. print(i ** 2)
  98. i = i+1
  99. print('Bye')
  100. # do-untile
  101. while True:
  102. #do something
  103. # check
  104. if xxxx: break
  105. # -
  106. # ## Break
  107. # As the name says. It is used to break out of a loop when a condition becomes true when executing the loop.
  108. for i in range(100):
  109. print i
  110. if i>=7:
  111. break
  112. # ## Continue
  113. # This continues the rest of the loop. Sometimes when a condition is satisfied there are chances of the loop getting terminated. This can be avoided using continue statement.
  114. for i in range(10):
  115. if i>4:
  116. print("The end.")
  117. continue
  118. elif i<7:
  119. print(i)
  120. # ## List Comprehensions
  121. # Python makes it simple to generate a required list with a single line of code using list comprehensions. For example If i need to generate multiples of say 27 I write the code using for loop as,
  122. res = []
  123. for i in range(1,11):
  124. x = 27*i
  125. res.append(x)
  126. print res
  127. # Since you are generating another list altogether and that is what is required, List comprehensions is a more efficient way to solve this problem.
  128. [27*x for x in range(1,11)]
  129. # That's it!. Only remember to enclose it in square brackets
  130. # Understanding the code, The first bit of the code is always the algorithm and then leave a space and then write the necessary loop. But you might be wondering can nested loops be extended to list comprehensions? Yes you can.
  131. [27*x for x in range(1,20) if x<=10]
  132. # Let me add one more loop to make you understand better,
  133. [27*z for i in range(50) if i==27 for z in range(1,11)]

机器学习越来越多应用到飞行器、机器人等领域,其目的是利用计算机实现类似人类的智能,从而实现装备的智能化与无人化。本课程旨在引导学生掌握机器学习的基本知识、典型方法与技术,通过具体的应用案例激发学生对该学科的兴趣,鼓励学生能够从人工智能的角度来分析、解决飞行器、机器人所面临的问题和挑战。本课程主要内容包括Python编程基础,机器学习模型,无监督学习、监督学习、深度学习基础知识与实现,并学习如何利用机器学习解决实际问题,从而全面提升自我的《综合能力》。