Comprehensive Python Cheat Sheet for Beginners

16 minute one-stop tutorial for getting started with Python

Rishi Sidhu
Apr 10, 2020 · 16 min read
Image for post
Image for post
Photo by Roman Synkevych on Unsplash

Introduction

Image for post
Image for post
Image for post
Image for post
Google Trends
Image for post
Image for post
Google Trends by Country

Python’s Community

Image for post
Image for post

Technically Speaking

Image for post
Image for post
Python Docs

Types

Basic Types

Integers

Image for post
Image for post

Complex, Float, Boolean, String and Byte

Image for post
Image for post

String

str1 = “I am a string” 
str2 = ‘I too am a string’
str3 = “”
str4 = str1 + ‘ ‘ + str2
Image for post
Image for post
0-Length strings
str5 = “Let’s say we need a single quote i.e. -> ‘ “ 
str6 = ‘What if we “need” a double quote i.e. -> “ ‘
Image for post
Image for post
str7 = “Let’s escape both single quote(\’) and double quote(\”)”
Image for post
Image for post
str8 = ‘’’Yet another way of escaping single (\’) and double (\”)’’’ 
str9 = “””Yet another way of escaping single (\’) and double (\”)”””
Image for post
Image for post
# Raw String 
str10 = R”Let’s escape both single quote(\’) and double quote(\”)”
Image for post
Image for post

Indexing

# Indexing String 
print(“str7[0] = “, str7[0])
print(“str7[1] = “, str7[1])
print(“str7[0:10] = “, str7[0:10])
print(“str7[10:5] = “, str7[10:5])
print(“str7[10:25] = “, str7[10:25])
Image for post
Image for post
String indexing

Type Conversion

# Type Conversion (Initial Types Used)
str1 = “15”
in1 = 10
fl1 = 10.8
Image for post
Image for post
Type Conversion

Operators

Numerical Operations

# Operators 
num1 = 10
num2 = 4
num3 = 5e2
num1 += 1
num1 *= 3
num1 /= 2
Image for post
Image for post

Things to note

Bitwise Operations

# Bitwise Operations 
num4 = 12
num5 = 9
bool1 = True
bool2 = False
Image for post
Image for post
Bitwise Operations

Data Structures

List

List Indexing and slicing

# List Indexing 
l1 = [1,2,3,4,5,6,7,8,9,10]
Image for post
Image for post
Notice the similarity with strings
Image for post
Image for post

Tuple

# Tuples 
tp1 = (“hello”, ) # Single length tuple
tp2 = (1,2,3,[5,6]) # Tuple containing mutable types
Image for post
Image for post
# Immutability 
tp2[0] = 0
Image for post
Image for post
Immutability
# Changing the mutable parts 
tp2[3][1] = 4
Image for post
Image for post

Set

# Sets 
set1 = {‘a’, ‘b’, ‘c’, ‘a’}
set2 = {‘a’, ‘d’, ‘e’, ‘f’}
set3 = {‘a’, ‘d’}
Image for post
Image for post
list1 = [‘a’,’b’, ‘a’, ‘a’] 
set1 = set(list1) #How to convert list to a set
Image for post
Image for post

Dictionary

# Dictionary 
dict1 = {‘a’: 12, ‘b’: 13, ‘c’:14, ‘d’:15}
dict2 = {‘d’ : 16, ‘e’: 17}
# Update old key and add new key
dict1.update(dict2)
Image for post
Image for post

Control Flow

If/Else

Image for post
Image for post

For Loop

Image for post
Image for post
str1 = “Hello World!” 
print(str1)
for c in str1:
print(c)
Image for post
Image for post

Break, Continue, Pass Statements

fruits = [“apple”, “banana”, “cherry”, “dragonfruit”, “elephant”, “fig”] 
print(“Available Fruits = “, fruits)
for fruit in fruits:
if(fruit == “elephant”):
print(“I cannot eat”, fruit)
break
else:
print(“I can eat”, fruit)
Image for post
Image for post
Image for post
Image for post

While Loop

Image for post
Image for post
# While Loop 
count = 0
while count < 5:
print(count)
count += 1

Functions

Image for post
Image for post

Named Arguments Vs Positional Arguments

Image for post
Image for post

Default Arguments

Image for post
Image for post

Lambda Functions

# Lambda Function 
x = lambda a : a/2
Image for post
Image for post

Range

Image for post
Image for post

Classes

Creating Class and Methods

Things to Note

Creating Objects

Image for post
Image for post

File Handling

# File Writing
filename = “test.txt”
file_handle = open(filename, “w”)
file_handle.write(“Yay!! File Created”)


# File Opening
file_handle = open(filename, “r”)
print(file_handle.read())
Image for post
Image for post
test.txt created

Libraries/Modules

PIP

Image for post
Image for post

Matplotlib

Image for post
Image for post

Pandas

import numpy as np 
import pandas as pd
house_data = pd.read_csv(“https://download.mlcc.google.com/mledu-datasets/california_housing_train.csv", sep=”,”)
print(house_data.head())
Image for post
Image for post
import matplotlib.pyplot as plt 
house_data[“median_income”].hist()
plt.show()
Image for post
Image for post
Matplotlib Histogram

Beautiful Soup — The Package to Scrape Web

Image for post
Image for post

Jupyter

Image for post
Image for post

Conclusion

The CodeHub

The hub for all things code

Rishi Sidhu

Written by

Imagine . Act . Realize | Artificial Intelligence, Books, Philosophy

The CodeHub

A place to start loving the coding life. Begin and enhance your coding life. We publish articles on the beauty of coding in a variety of languages like Python, R, Javascript etc.

Rishi Sidhu

Written by

Imagine . Act . Realize | Artificial Intelligence, Books, Philosophy

The CodeHub

A place to start loving the coding life. Begin and enhance your coding life. We publish articles on the beauty of coding in a variety of languages like Python, R, Javascript etc.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store