Python Cheatsheet ?
Python Cheatsheet ?
@ytyagi782
Python
Cheat Sheet
Yogesh Tyagi
@ytyagi782
Variables
We use variables to temporarily store
data in computer’s memory.
Comments
Receiving Input
Strings
We can define strings using single (* ) or double (* *) quotes.
To define a multi-line string, we surround our string with tripe quotes (****).
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 end index, the length of the string will be assumed.
Yogesh Tyagi
@ytyagi782
Arithmetic Operations
Operator precedence:
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.
List
We use dictionaries to store key / value pairs.
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.
Exceptions
Classes
We use classes to
define new types.
Inheritance
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!
Packages