Knowledge Base
Python Cheat Sheet for Beginners
How to use this cheat sheet — basics, operators, lists, dicts, NumPy, and strings.
Python Cheat Sheet for Beginners
# Python Cheat Sheet for Beginners
## How to use this cheat sheet
Python is the most popular programming language in data science. It is easy to learn and comes with a wide array of powerful libraries for data analysis. This cheat sheet provides beginners and intermediate users a guide to starting using Python. Use it to jump-start your journey with Python.
Related topics on this site: **AI** (automation scripts), **Linux** (running Python on servers), **DevOps** (CI/CD with Python tooling).
---
## Accessing help and getting object types
```python
'a' + 1 # Everything after # is ignored by Python
help(max) # Display documentation for max()
type('a') # Get the type of an object — returns <class 'str'>
```
---
## Importing packages
Python packages extend the language. Install with: `pip install pandas`
```python
import pandas # Import without alias
import pandas as pd # Import with alias
from pandas import DataFrame # Import an object from a package
```
---
## The working directory
The default path Python reads or saves files from. Requires the `os` library.
```python
import os
os.getcwd() # Get current directory
os.chdir('new/working/directory') # Set working directory
```
---
## Operators
### Arithmetic
```python
102 + 37 # Add with +
102 - 37 # Subtract with -
102 * 37 # Multiply with *
102 / 37 # Divide with /
22 // 7 # Integer divide with //
22 ** 7 # Raise to power with **
22 % 7 # Remainder with %
```
### Assignment
```python
a = 5
x[0] = 1 # Change value of list item
```
### Numeric comparison
```python
3 == 3 # Equality
3 != 3 # Inequality
3 > 1 # Greater than
3 >= 3 # Greater than or equal
3 < 4 # Less than
3 <= 4 # Less than or equal
```
### Logical
```python
~(a == b) # NOT with ~
(a != b) & (a < b) # AND with &
(a >= b) | (a < b) # OR with |
(a != b) ^ (a < b) # XOR with ^
```
---
## Lists
Ordered, changeable sequences. Zero-indexed.
```python
x = [1, 3, 2] # Create with []
# Functions and methods
sorted(x) # Sorted copy
x.sort() # Sort in-place
list(reversed(x)) # Reversed copy
x.reverse() # Reverse in-place
x.count(2) # Count occurrences of 2
# Indexing
x = ['a', 'b', 'c', 'd', 'e']
x[0] # First element
x[-1] # Last element
x[1:3] # 1st (inclusive) to 3rd (exclusive)
x[2:] # 2nd to end
x[:3] # 0th to 3rd (exclusive)
# Combine and repeat
x = [1, 3, 6]
y = [10, 15, 21]
x + y # [1, 3, 6, 10, 15, 21]
x * 3 # [1, 3, 6, 1, 3, 6, 1, 3, 6]
```
---
## Dictionaries
Key-value pairs. Keys must be unique.
```python
x = {'a': 1, 'b': 4, 'c': 9} # Create with {}
x.keys() # dict_keys(['a', 'b', 'c'])
x.values() # dict_values([1, 4, 9])
x['a'] # Get value by key → 1
```
---
## NumPy arrays
Scientific computing. `pip install numpy` then `import numpy as np`
```python
import numpy as np
np.array([1, 2, 3]) # List to array
np.arange(1, 5) # 1 to 4 (exclusive end)
np.arange(1, 5, 2) # [1, 3]
np.repeat([1, 3, 6], 3) # Repeat each value 3x
np.tile([1, 3, 6], 3) # Repeat whole array 3x
# Math (all take array input)
np.mean(x) np.var(x) np.std(x)
np.max(x) np.min(x) np.sum(x)
np.log(x) np.exp(x)
np.round(x, n)
np.quantile(x, q)
```
---
## Strings
```python
"DataCamp"
'He said, "DataCamp"' # Escape quotes with \
"""
Multi-line
strings
"""
str[0] # Character at position
str[0:2] # Substring (start inclusive, end exclusive)
# Combine and split
"Data" + "Framed" # Concatenate
"data " * 3 # Repeat
"beekeepers".split('e') # ['b', '', 'k', '', 'p', 'rs']
# Mutate
str.upper() str.lower() str.title()
str.replace('J', 'P')
```
DevOps
AI
Kubernetes
Splunk
Prometheus
Linux
Cloud Providers
Python