Python keywords.docx - Python keywords last modified July 6...

This preview shows page 1 - 4 out of 15 pages.

Python keywords last modified July 6, 2020 In this part of the Python programming tutorial, we cover keywords in Python language. Python keyword Python keyword is a special word that forms the vocabulary of the Python language. It is a reserved word that cannot be used as an identifier. Python keywords list The following is a list of keywords for the Python programming language. False def if raise None del import return True elif in try and else is while as except lambda with assert finally nonlocal yield break for not class from or continue global pass Python is a dynamic language. It changes during time. The list of keywords may change in the future. keywords.py #!/usr/bin/env python # keywords.py import sys import keyword print("Python version: ", sys.version_info) print("Python keywords: ", keyword.kwlist) This script prints the version of Python and its actual keyword list. $ ./keywords.py
Python version: sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0) Python keywords: ['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'] The output show the list of Python keywords for Python 3.5.2. Python control flow The while keyword is a basic keyword for controlling the flow of the program. The statements inside the while loop are executed until the expression evaluates to False . while_kwd.py #!/usr/bin/env python # while_kwd.py numbers = [22, 34, 12, 32, 4] mysum = 0 i = len(numbers) while i != 0: i -= 1 mysum = mysum + numbers[i] print("The sum is:", mysum) In our script we want to calculate the sum of all values in the numbers list. We utilize the while loop. We determine the length of the list. The while loop is executed over and over again, until the i is equal to zero. In the body of the loop, we decrement the counter and calculate the sum of values. $ ./while_kwd.py The sum is: 104 The sum of values is 104. The break keyword is used to interrupt the cycle if needed. break_kwd.py
#!/usr/bin/env python # break_kwd.py import random while True: val = random.randint(1, 30) print(val, end=" ") if val == 22: break print() In our example, we print random integer numbers. If the number equals to 22, the cycle is interrupted with the break keyword. $ ./break_kwd.py 14 14 30 16 16 20 23 15 17 22 The next example shows the continue keyword. It is used to interrupt the current cycle, without jumping out of the whole cycle. It initiates a new cycle. continue_kwd.py #!/usr/bin/env python # continue_kwd.py num = 0 while num < 1000: num = num + 1 if num % 2 == 0: continue print(num, end=" ") In the example we print all numbers smaller than 1000 that cannot be divided by number 2 without a remainder. The if keyword is a common control flow keyword. It is used to determine which statements are going to be executed.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture