0% found this document useful (0 votes)
3 views

Python Cheat Sheet 2

The document discusses Python basics including lists, scopes, classes and objects, functions, modules, and more. Lists can store multiple items in a variable and support indexing, slicing, and built-in methods. Functions define reusable blocks of code. Classes are templates for objects with attributes and methods. Modules allow importing functionality from other Python files.

Uploaded by

jimenezchopo9
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python Cheat Sheet 2

The document discusses Python basics including lists, scopes, classes and objects, functions, modules, and more. Lists can store multiple items in a variable and support indexing, slicing, and built-in methods. Functions define reusable blocks of code. Classes are templates for objects with attributes and methods. Modules allow importing functionality from other Python files.

Uploaded by

jimenezchopo9
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

PYTHON : BASICS II

Lists Store multiple items in a single variable Scope Where in the program a variable is visible

snow = [0.3, 0.0, 0.0, 1.2, 3.9, 2.2, 0.8] t = 29 # Global scope

# Index def func():


day_1 = snow[0] # 0.3 t = 42 # Local scope
day_7 = snow[6] # 0.8 print(t)

# Negative index print(t) # Output: 29


day_7 = snow[-1] # 0.8 func() # Output: 42

# Slicing
snow_weekday = snow[0:5] Classes & Objects A class is a template for the objects
snow_weekend = snow[5:7]
class Person:
def __init__(self, name, age):
self.name = name
List Functions & Methods Update lists or analyze list items self.age = age

film_runtimes = [121, 142, 131, 124] def say_hi(self):


print(f' 👋
My name is {self.name}')
# Built-in functions
len(film_r
untimes) # Output: 4 brandon = Person('Brandon', 31)
max(film_r
untimes) # Output: 142 kat = Person('Katherin', 24)
min(film_r
untimes) # Output: 121
brandon.say_hi() # 👋 My name is Brandon
# Built-in methods kat.say_hi() # 👋 My name is Katherin
film_r
untimes.append(152)
# [121, 142, 131, 124, 152]
film_r
untimes.insert(3, 138)
Modules Python file that can be imported into another
# [121, 142, 131, 138, 124, 152]
film_r
untimes.remove(142) from matplotlib import plotly as plt
# [121, 131, 138, 124, 152] import random
film_r
untimes.pop(0)
# [131, 138, 124, 152] print(random.randint(1, 10))

x = [1, 2, 3]
y = [4, 6, 8]
Functions Define once to use multiple times

def greetings(): plt.plot(x, y)


return 'Hello, World!' plt.show()

print(greetings()) # Output: Hello, World!

Notes

Parameters & Arguments Affects outcome of functions

def add(x, y):


return x + y

print(add(2, 3)) # Output: 5


print(add(21, 56)) # Output: 77

Made with by

You might also like