100% found this document useful (1 vote)
391 views

Begginer's Python Cheat Sheet-Dataquest

This document provides a cheat sheet for Python basics including variables, data types, strings, lists, dictionaries, conditionals, functions, and more. It covers key syntax and functions for working with files, math operations, Boolean comparisons, and loops. The cheat sheet is intended to help users learn Python fundamentals for data science and directs them to a free online learning platform.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
391 views

Begginer's Python Cheat Sheet-Dataquest

This document provides a cheat sheet for Python basics including variables, data types, strings, lists, dictionaries, conditionals, functions, and more. It covers key syntax and functions for working with files, math operations, Boolean comparisons, and loops. The cheat sheet is intended to help users learn Python fundamentals for data science and directs them to a free online learning platform.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

LEARN DATA SCIENCE ONLINE

Start Learning For Free - www.dataquest.io

Data Science Cheat Sheet


Python Basics

BASICS, PRINTING AND GETTING HELP


x = 3 - Assign 3 to the variable x help(x) - Show documentation for the str data type
print(x) - Print the value of x help(print) - Show documentation for the print() function
type(x) - Return the type of the variable x (in this case, int for integer)

READING FILES 3 ** 2 - Raise 3 to the power of 2 (or 32) def calculate(addition_one,addition_two,


f = open("my_file.txt","r") 27 ** (1/3) - The 3rd root of 27 (or 3√27) exponent=1,factor=1):
file_as_string = f.read() x += 1 - Assign the value of x + 1 to x result = (value_one + value_two) ** exponent * factor
- Open the file my_file.txt and assign its x -= 1 - Assign the value of x - 1 to x return result
contents to s - Define a new function calculate with two
import csv L I STS required and two optional named arguments
f = open("my_dataset.csv","r") l = [100,21,88,3] - Assign a list containing the which calculates and returns a result.
csvreader = csv.reader(f) integers 100, 21, 88, and 3 to the variable l addition(3,5,factor=10) - Run the addition
csv_as_list = list(csvreader) l = list() - Create an empty list and assign the function with the values 3 and 5 and the named
- Open the CSV file my_dataset.csv and assign its result to l argument 10
data to the list of lists csv_as_list l[0] - Return the first value in the list l
l[-1] - Return the last value in the list l B O O L E A N C O M PA R I S O N S
ST R I N G S l[1:3] - Return a slice (list) containing the second x == 5 - Test whether x is equal to 5
s = "hello" - Assign the string "hello" to the and third values of l x != 5 - Test whether x is not equal to 5
variable s len(l) - Return the number of elements in l x > 5 - Test whether x is greater than 5
s = """She said, sum(l) - Return the sum of the values of l x < 5 - Test whether x is less than 5
"there's a good idea." min(l) - Return the minimum value from l x >= 5 - Test whether x is greater than or equal to 5
""" max(l) - Return the maximum value from l x <= 5 - Test whether x is less than or equal to 5
- Assign a multi-line string to the variable s. Also l.append(16) - Append the value 16 to the end of l x == 5 or name == "alfred" - Test whether x is
used to create strings that contain both " and ' l.sort() - Sort the items in l in ascending order equal to 5 or name is equal to "alfred"
characters " ".join(["A","B","C","D"]) - Converts the list x == 5 and name == "alfred" - Test whether x is
len(s) - Return the number of characters in s ["A", "B", "C", "D"] into the string "A B C D" equal to 5 and name is equal to "alfred"
s.startswith("hel") - Test whether s starts with 5 in l - Checks whether the value 5 exists in the list l
the substring "hel" DICTIONARIES "GB" in d - Checks whether the value "GB" exists in
s.endswith("lo") - Test whether s ends with the d = {"CA":"Canada","GB":"Great Britain", the keys for d
substring "lo" "IN":"India"} - Create a dictionary with keys of
"{} plus {} is {}".format(3,1,4) - Return the "CA", "GB", and "IN" and corresponding values I F STAT E M E N TS A N D LO O P S
string with the values 3, 1, and 4 inserted of of "Canada", "Great Britain", and "India" The body of if statements and loops are defined
s.replace("e","z") - Return a new string based d["GB"] - Return the value from the dictionary d through indentation.
on s with all occurances of "e" replaced with "z" that has the key "GB" if x > 5:
s.split(" ") - Split the string s into a list of d.get("AU","Sorry") - Return the value from the print("{} is greater than five".format(x))
strings, separating on the character " " and dictionary d that has the key "AU", or the string elif x < 0:
return that list "Sorry" if the key "AU" is not found in d print("{} is negative".format(x))
d.keys() - Return a list of the keys from d else:
NUMERIC TYPES AND d.values() - Return a list of the values from d print("{} is between zero and five".format(x))
M AT H E M AT I C A L O P E R AT I O N S d.items() - Return a list of (key, value) pairs - Test the value of the variable x and run the code
i = int("5") - Convert the string "5" to the from d body based on the value
integer 5 and assign the result to i for value in l:
f = float("2.5") - Convert the string "2.5" to MODULES AND FUNCTIONS print(value)
the float value 2.5 and assign the result to f The body of a function is defined through - Iterate over each value in l, running the code in
5 + 5 - Addition indentation. the body of the loop with each iteration
5 - 5 - Subtraction import random - Import the module random while x < 10:
10 / 2 - Division from math import sqrt - Import the function x += 1
5 * 2 - Multiplication sqrt from the module math - Run the code in the body of the loop until the
value of x is no longer less than 10

LEARN DATA SCIENCE ONLINE


Start Learning For Free - www.dataquest.io

You might also like