Flashcards › Technology & Coding › Python
Python Syntax and Built-ins
Everyday Python statements, functions and data structures.
0 of 75 known
75 cards
- Print text to the screenTaskprint('Hello')
- Get input from the userTaskinput('Name? ')
- Make a variableTaskx = 5
- Comment a lineTask# this is a comment
- Integer divisionTask7 // 2 gives 3
- Remainder (modulo)Task7 % 2 gives 1
- Exponent (power)Task2 ** 3 gives 8
- Not equal comparisonTaskx != y
- Equal comparisonTaskx == y
- Logical andTaska and b
- Logical orTaska or b
- Logical notTasknot a
- If statementTaskif x > 3:
- Else ifTaskelif x == 3:
- ElseTaskelse:
- For loop over a range of 5Taskfor i in range(5):
- Range from 1 to 10Taskrange(1, 11)
- While loopTaskwhile x < 10:
- Stop a loop earlyTaskbreak
- Skip to the next loop passTaskcontinue
- Do nothing placeholderTaskpass
- Define a functionTaskdef greet(name):
- Return a valueTaskreturn x
- Default parameterTaskdef f(x=10):
- Lambda (small anonymous function)Tasklambda x: x * 2
- Make a listTasknums = [1, 2, 3]
- Add to the end of a listTasknums.append(4)
- Remove last item of a listTasknums.pop()
- Length of a list or stringTasklen(nums)
- First item of a listTasknums[0]
- Last item of a listTasknums[-1]
- Slice the first two itemsTasknums[:2]
- Sort a list in placeTasknums.sort()
- Sorted copy of a listTasksorted(nums)
- Reverse a listTasknums.reverse() or nums[::-1]
- Tuple (unchangeable sequence)Taskt = (1, 2, 3)
- Set (unique items)Tasks = {1, 2, 3}
- DictionaryTaskd = {'a': 1, 'b': 2}
- Get a dictionary valueTaskd['a']
- Loop over dictionary itemsTaskfor k, v in d.items():
- Check if item is in a listTask3 in nums
- Convert to integerTaskint('5')
- Convert to stringTaskstr(5)
- Convert to floatTaskfloat('3.5')
- Convert to listTasklist('abc')
- Type of a valueTasktype(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 stringTasks.replace('a', 'b')
- Strip whitespaceTasks.strip()
- F-stringTaskf'Hello {name}'
- Multi-line stringTask'''text over lines'''
- Import a moduleTaskimport math
- Import one nameTaskfrom math import sqrt
- Random integerTaskimport random; random.randint(1, 6)
- Square rootTaskmath.sqrt(16)
- Absolute valueTaskabs(-3)
- Round a numberTaskround(3.6)
- Maximum of valuesTaskmax(1, 5, 3)
- Sum of a listTasksum(nums)
- List comprehensionTask[x * 2 for x in nums]
- Open and read a fileTaskwith open('f.txt') as f: data = f.read()
- Handle errorsTasktry: ... except ValueError: ...
- Raise an errorTaskraise ValueError('bad')
- Define a classTaskclass Dog:
- Constructor methodTaskdef __init__(self, name):
- The current object inside a methodTaskself
- None valueTaskNone
- Boolean valuesTaskTrue and False
- Enumerate with indexTaskfor i, x in enumerate(nums):
- Zip two listsTaskzip(a, b)
- Check if a value is NoneTaskx is None
- Run code only when file is the main programTaskif __name__ == '__main__':
- Python's creatorTaskGuido van Rossum, who released Python in 1991
Download this deck
- CSVExcel, Google Sheets, Numbers
- Anki / Quizlet (tab-separated)Import straight into Anki or Quizlet
- JSONFor programmers and apps
- Plain textOne card a line, “front : back”, ready for Peabody's flashcard maker to print
Want paper cards? Paste the plain-text download into the flashcard maker.