Flashcards › Technology & Coding › Python

Python Syntax and Built-ins

Everyday Python statements, functions and data structures.

75 cardsGrades 6–8, Grades 9–12, College, Adult#Python#programming#syntax

0 of 75 known
75 cards
  • Print text to the screenTask
    print('Hello')
  • Get input from the userTask
    input('Name? ')
  • Make a variableTask
    x = 5
  • Comment a lineTask
    # this is a comment
  • Integer divisionTask
    7 // 2 gives 3
  • Remainder (modulo)Task
    7 % 2 gives 1
  • Exponent (power)Task
    2 ** 3 gives 8
  • Not equal comparisonTask
    x != y
  • Equal comparisonTask
    x == y
  • Logical andTask
    a and b
  • Logical orTask
    a or b
  • Logical notTask
    not a
  • If statementTask
    if x > 3:
  • Else ifTask
    elif x == 3:
  • ElseTask
    else:
  • For loop over a range of 5Task
    for i in range(5):
  • Range from 1 to 10Task
    range(1, 11)
  • While loopTask
    while x < 10:
  • Stop a loop earlyTask
    break
  • Skip to the next loop passTask
    continue
  • Do nothing placeholderTask
    pass
  • Define a functionTask
    def greet(name):
  • Return a valueTask
    return x
  • Default parameterTask
    def f(x=10):
  • Lambda (small anonymous function)Task
    lambda x: x * 2
  • Make a listTask
    nums = [1, 2, 3]
  • Add to the end of a listTask
    nums.append(4)
  • Remove last item of a listTask
    nums.pop()
  • Length of a list or stringTask
    len(nums)
  • First item of a listTask
    nums[0]
  • Last item of a listTask
    nums[-1]
  • Slice the first two itemsTask
    nums[:2]
  • Sort a list in placeTask
    nums.sort()
  • Sorted copy of a listTask
    sorted(nums)
  • Reverse a listTask
    nums.reverse() or nums[::-1]
  • Tuple (unchangeable sequence)Task
    t = (1, 2, 3)
  • Set (unique items)Task
    s = {1, 2, 3}
  • DictionaryTask
    d = {'a': 1, 'b': 2}
  • Get a dictionary valueTask
    d['a']
  • Loop over dictionary itemsTask
    for k, v in d.items():
  • Check if item is in a listTask
    3 in nums
  • Convert to integerTask
    int('5')
  • Convert to stringTask
    str(5)
  • Convert to floatTask
    float('3.5')
  • Convert to listTask
    list('abc')
  • Type of a valueTask
    type(x)
  • Join strings in a listTask
    ', '.join(words)
  • Split a string into a listTask
    'a b c'.split()
  • Uppercase a stringTask
    'hi'.upper()
  • Replace text in a stringTask
    s.replace('a', 'b')
  • Strip whitespaceTask
    s.strip()
  • F-stringTask
    f'Hello {name}'
  • Multi-line stringTask
    '''text over lines'''
  • Import a moduleTask
    import math
  • Import one nameTask
    from math import sqrt
  • Random integerTask
    import random; random.randint(1, 6)
  • Square rootTask
    math.sqrt(16)
  • Absolute valueTask
    abs(-3)
  • Round a numberTask
    round(3.6)
  • Maximum of valuesTask
    max(1, 5, 3)
  • Sum of a listTask
    sum(nums)
  • List comprehensionTask
    [x * 2 for x in nums]
  • Open and read a fileTask
    with open('f.txt') as f: data = f.read()
  • Handle errorsTask
    try: ... except ValueError: ...
  • Raise an errorTask
    raise ValueError('bad')
  • Define a classTask
    class Dog:
  • Constructor methodTask
    def __init__(self, name):
  • The current object inside a methodTask
    self
  • None valueTask
    None
  • Boolean valuesTask
    True and False
  • Enumerate with indexTask
    for i, x in enumerate(nums):
  • Zip two listsTask
    zip(a, b)
  • Check if a value is NoneTask
    x is None
  • Run code only when file is the main programTask
    if __name__ == '__main__':
  • Python's creatorTask
    Guido van Rossum, who released Python in 1991

Download this deck

Want paper cards? Paste the plain-text download into the flashcard maker.