- Python Oop Cheat Sheet Download
- Python Syntax Cheat
- Best Python Cheat Sheet Pdf
- Python Class Cheat Sheet
Resource
Download
- See also Python Programming - Getting Started. Following are terse descriptions for Python3 keywords, programming concepts, and commonly used functions. For more information, see w3schools for a bit more explanation and python.org for the language reference.
- Photo by Hitesh Choudhary on Unsplash. In the previous installments of the Zero to Hero Python series, we covered. Dealing with primitive data types like integers, strings, and boolean values.
String
String Methods
File
Methods
Object-oriented programming (OOP) is a method of structuring a program by bundling related properties and behaviors into individual objects. In this tutorial, you’ll learn the basics of object-oriented programming in Python. Conceptually, objects are like the components of a system. Think of a program as a factory assembly line of sorts. OOP Terminology in Python Class: A blueprint to create objects. It defines the data (attributes) and functionality (methods) of the objects. You can access both attributes and methods via the dot notation.
Attributes
Set & Mapping
Set Types
- len(s)
- x in s
- x not in s
Mapping Types
- len(d)
- d[key]
- d[key] = value
- del d[key]
- key in d
- key not in d
- iter(d)
Date Time
Date Object
Datetime Object
Time Object
Array
Array Methods
Indexes and Slices
- a=[0,1,2,3,4,5]
- 6
- len(a)
- 0
- a[0]
- 5
- a[5]
- 5
- a[-1]
- 4
- a[-2]
- [1,2,3,4,5]
- a[1:]
- [0,1,2,3,4]
- a[:5]
- [0,1,2,3]
- a[:-2]
- [1,2]
- a[1:3]
- [1,2,3,4]
- a[1:-1]
- Shallow copy of a
- b=a[:]
Math
Number Theoretic
Power and Logarithmic
Trigonometric Functions
Hyperbolic Functions
Constants
- math.pi
- The mathematical constant π = 3.141592..., to available precision.
- math.e
- The mathematical constant e = 2.718281..., to available precision.
Random
Functions
Sys
Sys Variables
- argv
- Command line args
- builtin_module_names
- Linked C modules
- byteorder
- Native byte order
- check_-interval
- Signal check frequency
- exec_prefix
- Root directory
- executable
- Name of executable
- exitfunc
- Exit function name
- modules
- Loaded modules
- path
- Search path
- platform
- Current platform
- stdin, stdout, stderr
- File objects for I/O
- version_info
- Python version info
- winver
- Version number
sys.argv
- foo.py
- sys.argv[0]
- bar
- sys.argv[1]
- -c
- sys.argv[2]
- qux
- sys.argv[3]
- --h
- sys.argv[4]
OS
os Variables
- altsep
- Alternative sep
- curdir
- Current dir string
- defpath
- Default search path
- devnull
- Path of null device
- extsep
- Extension separator
- linesep
- Line separator
- name
- Name of OS
- pardir
- Parent dir string
- pathsep
- Patch separator
- sep
- Path separator
Class
Special Methods
- __new__(cls)
- __lt__(self, other)
- __init__(self, args)
- __le__(self, other)
- __del__(self)
- __gt__(self, other)
- __repr__(self)
- __ge__(self, other)
- __str__(self)
- __eq__(self, other)
- __cmp__(self, other)
- __ne__(self, other)
- __index__(self)
- __nonzero__(self)
- __hash__(self)
- __getattr__(self, name)
- __getattribute__(self, name)
- __setattr__(self, name, attr)
- __delattr__(self, name)
- __call__(self, args, kwargs)
String Formatting
Formatting Operations
- 'd'
- Signed integer decimal.
- 'i'
- Signed integer decimal.
- 'o'
- Signed octal value.
- 'u'
- Obsolete type – it is identical to 'd'.
- 'x'
- Signed hexadecimal (lowercase).
- 'X'
- Signed hexadecimal (uppercase).
- 'e'
- Floating point exponential format (lowercase).
- 'E'
- Floating point exponential format (uppercase).
- 'f'
- Floating point decimal format.
- 'F'
- Floating point decimal format.
- 'g'
- Floating point format. Uses lowercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise.
- 'G'
- Floating point format. Uses uppercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise.
- 'c'
- Single character (accepts integer or single character string).
- 'r'
- String (converts any Python object using repr().
- 's'
- String (converts any Python object using str()
- '%'
- No argument is converted, results in a '%' character in the result.
Date Formatting
Date Formatting
- %a
- Abbreviated weekday (Sun)
- %A
- Weekday (Sunday)
- %b
- Abbreviated month name (Jan)
- %B
- Month name (January)
- %c
- Date and time
- %d
- Day (leading zeros) (01 to 31)
- %H
- 24 hour (leading zeros) (00 to 23)
- %I
- 12 hour (leading zeros) (01 to 12)
- %j
- Day of year (001 to 366)
- %m
- Month (01 to 12)
- %M
- Minute (00 to 59)
- %p
- AM or PM
- %S
- Second (00 to 61?)
- %U
- Week number1 (00 to 53)
- %w
- Weekday2 (0 to 6)
- %W
- Week number3 (00 to 53)
- %x
- Date
- %X
- Time
- %y
- Year without century (00 to 99)
- %Y
- Year (2008)
- %Z
- Time zone (GMT)
- %%
- A literal '%' character (%)
Q: What’s the object-oriented way to become wealthy?
A: Inheritance. ?
A: Inheritance. ?
Your vocabulary determines the reality of your life.
In this tutorial, I have compiled the most essential terms and concepts of object-oriented programming in Python. My goal was to create the best Python OOP cheat sheet that shows them in one place.
Well — here it is:
Before we dive in the code from the Python OOP cheat sheet, let’s swipe through the most important OOP concepts in this Instagram post:
Ein Beitrag geteilt von The Python Blog (@finxter.com_) am
Want to get more printable PDF cheat sheets like the following one?
Join my free email series with cheat sheets, free Python lessons, and continuous improvement in Python! It’s fun! ?
So let’s study the code!
Before we dive into the vocabulary, here’s an interactive Python shell:
Exercise: Create a new class Tiger that inherits from the parent class Cat and add a custom method!
Let’s dive into the Vocabulary!
OOP Terminology in Python
Class: A blueprint to create objects. It defines the data (attributes) and functionality (methods) of the objects. You can access both attributes and methods via the dot notation.
Object (=instance): A piece of encapsulated data with functionality in your Python program that is built according to a class definition. Often, an object corresponds to a thing in the real world. An example is the object
Obama
that is created according to the class definition Person
. An object consists of an arbitrary number of attributes and methods, encapsulated within a single unit.Instantiation: The process of creating an object of a class.
Method: A subset of the overall functionality of an object. The method is defined similarly to a function (using the keyword
def
) in the class definition. An object can have an arbitrary number of methods.Method overloading: You may want to define a method in a way so that there are multiple options to call it. For example for class
X
, you define a method f(…)
that can be called in three ways: f(a)
, f(a,b)
, or f(a,b,c)
. To this end, you can define the method with default parameters (e.g., f(a, b=None, c=None)
).Attribute: A variable defined for a class (class attribute) or for an object (instance attribute). You use attributes to package data into enclosed units (class or instance).
Class attribute (=class variable, static variable, static attribute): A variable that is created statically in the class definition and that is shared by all class objects.
Dynamic attribute: An “–>instance attribute” that is defined dynamically during the execution of the program and that is not defined within any method. For example, you can simply add a new attribute
neew
to any object o by calling o.neew = ...
.Instance attribute (=instance variable): A variable that holds data that belongs only to a single instance. Other instances do not share this variable (in contrast to “–>class attributes”). In most cases, you create an instance attribute
x
in the constructor when creating the instance itself using the self
keywords (e.g., self.x = ...
).Inheritance: Class
A
can inherit certain characteristics (like attributes or methods) from class B
. For example, the class Dog
may inherit the attribute number_of_legs
from the class Animal
. In this case, you would define the inherited class Dog
as follows: class Dog(Animal): ...
Encapsulation: Binding together data and functionality that manipulates the data.
If you have understood these OOP terms, you can follow most of the discussions about object-oriented programming. That’s the first step towards proficiency in Python!
Automate Your Learning Progress in Python
Thanks for reading that far—you’re clearly ambitious in mastering the Python programming language.
For your convenience, I have created a Python cheat sheet email series where you will get tons of free stuff (cheat sheets, PDFs, lessons, code contests). Join 5,253 subscribers. It’s fun!
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
Python Oop Cheat Sheet Download
Related Articles:
Python Syntax Cheat
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
Best Python Cheat Sheet Pdf
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.