Yogesh Tyagi
@ytyagi782
Python
Cheat Sheet
Yogesh Tyagi
@ytyagi782
Variables
We use variables to temporarily store
data in computer’s memory.
price is an integer (a whole number without a decimal point)
rating is a float (a number with a decimal point)
course_name is a string (a sequence of characters)
is_published is a boolean. Boolean values can be True or False.
Yogesh Tyagi
@ytyagi782
Comments
We use comments to add notes to our code. Good
comments explain the hows and whys, not what the
code does. That should be reflected in the code itself.
Use comments to add reminders to yourself or other
developers, or also explain your assumptions and the
reasons you've written code in a certain way.
Yogesh Tyagi
@ytyagi782
Receiving Input
We can receive input from the user by
calling the input() function.
The input function always returns data as a
string. So, we're converting the result into an
integer by calling the built-in intO function.
Yogesh Tyagi
@ytyagi782
Strings
We can define strings using single (* ) or double (* *) quotes.
To define a multi-line string, we surround our string with tripe quotes (****).
We can get individual characters in a string using square brackets [].
We can slice a string using a similar notation:
The above expression returns all the characters starting from the
index position of 1 to 5 (but excluding 5). The result will be ytho
if we leave out the start index, 0 will be assumed.
If we leave out the end index, the length of the string will be assumed.
Yogesh Tyagi
@ytyagi782
We can use formatted strings to dynamically insert values into our
strings:
To check if a string contains a character (or a sequence of
characters), we use the in operator;
Yogesh Tyagi
@ytyagi782
Arithmetic Operations
Augmented assignment operator:
Operator precedence:
1. Parenthesis 3. Multiplication / Division
2. Exponentiation 4. Addition / Substraction
Yogesh Tyagi
@ytyagi782
If Statements
Logical Operators:
Comparison Operators
a>b
a ›= b (greater than or equal to)
a<b
a <= b
a == b (equals)
a != b (not equals)
Yogesh Tyagi
@ytyagi782
While Loops
For loops
•range(5): generates 0, 1, 2, 3, 4
•range(1, 5): generates 1, 2, 3, 4
•range(1, 5, 2): generates 1, 3
Yogesh Tyagi
@ytyagi782
List
Tuples
They are like read-only lists. We use them to store a list of items. But once
we define a tuple, we cannot add or remove items or change the existing
items.
We can unpack a list or a tuple into separate variables:
Yogesh Tyagi
@ytyagi782
List
We use dictionaries to store key / value pairs.
We can use strings or numbers to define keys. They
should be unique. We can use any types for the values.
Yogesh Tyagi
@ytyagi782
Funcitons
We use functions to break up our code into small chunks.
These chunks are easier to read, understand and maintain.
If there are bugs, it's easier to find bugs in a small chunk
than the entire program. We can also re-use these chunks.
Parameters are placeholders for the data we can pass to
functions. Arguments are the actual values we pass.
We have two types of arguments:
Positional arguments: their position (order) matters
Keywords arguments: position doesn’t matter - we
prefix them with the parameter name.
Yogesh Tyagi
@ytyagi782
Our functions can return values. If we don't use
the return statement, by default None is returned.
None is an object that represents the absence of
a value.
Yogesh Tyagi
@ytyagi782
Exceptions
Exceptions are errors that crash our programs. They
often happen because of bad input or programming
errors. It’s our job to anticipate and handle these
exceptions to prevent our programs from cashing.
Yogesh Tyagi
@ytyagi782
Classes
We use classes to
define new types.
When a function is part of a class, we refer to it as a method.
Classes define templates or blueprints for creating objects. An
object is an instance of a class. Every time we create a new
instance, that instance follows the structure we define using
the class.
__ init__ is a special method called constructor. It gets called
at the time of creating new objects. We use it to initialize our
objects.
Yogesh Tyagi
@ytyagi782
Inheritance
Inheritance is a technique to remove code duplication.
We can create a base class to define the common
methods and then have other classes inherit these
methods.
Yogesh Tyagi
@ytyagi782
Modules
A module is a file with some Python code. We use
modules to break up our program into multiple files.
This way, our code will be better organized. We won't
have one gigantic file with a million lines of code in it!
There are 2 ways to import modules: we can import the
entire module, or specific objects in a module.
Yogesh Tyagi
@ytyagi782
Packages
A package is a directory with __init __.py in it. It
can contain one or more modules.
Yogesh Tyagi
@ytyagi782
Python Standard Library
Python comes with a huge library of modules for
performing common tasks such as sending
emails, working with date/time, generating
random values, etc.
Yogesh Tyagi
@ytyagi782
Follow for More