Python Cheat Sheet - Classes “ A puzzle a day to learn, code, and play ” → Visit f inxter.com Description Example Classes A class encapsulates data and functionality - data as attributes, and functionality as methods. It is a blueprint to create concrete instances in the memory. Advance Python Cheat sheet from beginners to advance 2020. With the help This cheat sheet You will be able to Understand Python basic with reference.You can download Free these Python cheats Python Cheat Sheet for Lists Python Cheat Sheet for dictionaries. Python Cheat Sheet - Complex Data Types “ A puzzle a day to learn, code, and play ” → Visit f inxter.com Description Example List A container data type that stores a sequence of elements. Unlike strings, lists are mutable: modification possible. L = 1, 2, 2 print(len(l)) # 3 Adding elements. File = open('Hello.txt', 'a') # open in append mode file.write('Hello World again') file.close.
Python Cheat Sheet Pdf
Basics
Print a number | print(123) |
Print a string | print('test') |
Adding numbers | print(1+2) |
Variable assignment | number = 123 |
Print a variable | print(number) |
Function call | x = min(1, 2) |
Comment | # a comment |
Types
Integer | 42 |
String | 'a string' |
List | [1, 2, 3] |
Tuple | (1, 2, 3) |
Boolean | True |
Useful functions
Write to the screen | print('hi') |
Calculate length | len('test') |
Minimum of numbers | min(1, 2) |
Maximum of numbers | max(1, 2) |
Cast to integer | int('123') |
Cast to string | str(123) |
Cast to boolean | bool(1) |
Range of numbers | range(5, 10) |
Other syntax
Return a value | return 123 |
Indexing | 'test'[0] |
Slicing | 'test'[1:3] |
Continue to next loop iteration | continue |
Exit the loop | break |
List append | numbers = numbers + [4] |
List append (with method call) | numbers.append(4) |
List item extraction | value = numbers[0] |
List item assignment | numbers[0] = 123 |
Terminology
syntax | the arrangement of letters and symbols in code |
program | a series of instructions for the computer |
write text to the screen | |
string | a sequence of letters surrounded by quotes |
variable | a storage space for values |
value | examples: a string, an integer, a boolean |
assignment | using = to put a value into a variable |
function | a machine you put values into and values come out |
call (a function) | to run the code of the function |
argument | the input to a function call |
parameter | the input to a function definition |
return value | the value that is sent out of a function |
conditional | an instruction that's only run if a condition holds |
loop | a way to repeatedly run instructions |
list | a type of value that holds other values |
tuple | like a list, but cannot be changed |
indexing | extracting one element at a certain position |
slicing | extracting some elements in a row |
dictionary | a mapping from keys to values |
Reminders
- Strings and lists are indexed starting at 0, not 1
- Print and return are not the same concept
- The return keyword is only valid inside functions
- Strings must be surrounded by quotes
- You cannot put spaces in variable or function names
- You cannot add strings and integers without casting
- Consistent indentation matters
- Use a colon when writing conditionals, function definitions, and loops
- Descriptive variable names help you understand your code better
Conditionals
Lists
Defining functions
Loops
Dictionaries
Comparisons
Equals | |
Not equals | != |
Less than | < |
Less than or equal | <= |
Greater than | > |
Useful methods
String to lowercase | 'xx'.lower() |
String to uppercase | 'xx'.upper() |
Split string by spaces | 'a b c'.split(' ') |
Remove whitespace around string | ' a string '.strip() |
Combine strings into one string | ' '.join(['a', 'b']) |
String starts with | 'xx'.startswith('x') |
String ends with | 'xx'.endswith('x') |
List count | [1, 2].count(2) |
List remove | [1, 2].remove(2) |
Dictionary keys | {1: 2}.keys() |
Dictionary values | {1: 2}.values() |
Dictionary key/value pairs | {1: 2}.items() |
Other neat bonus stuff
Zip lists | zip([1, 2], ['one', 'two']) |
Set | my_set = {1, 2, 3} |
Set intersection | {1, 2} & {2, 3} |
Set union | {1, 2} | {2, 3} |
Index of list element | [1, 2, 3].index(2) |
Sort a list | numbers.sort() |
Reverse a list | numbers.reverse() |
Sum of list | sum([1, 2, 3]) |
Numbering of list elements | for i, item in enumerate(items): |
Read a file line by line | for line in open('file.txt'): |
Read file contents | contents = open('file.txt').read() |
Random number between 1 and 10 | import random; x = random.randint(1, 10) |
List comprehensions | [x+1 for x in numbers] |
Check if any condition holds | any([True, False]) |
Check if all conditions hold | all([True, False]) |
Missing something?
Let us know here.Advance Python Cheat sheet from beginners to advance 2020. With the help This cheat sheet You will be able to Understand Python basic with reference.You can download Free these Python cheats
Python Cheat Sheet for Lists
Python Cheat Sheet for dictionaries
Python Cheat Sheet for if statements and while loops
Python Cheat Sheet for functions
Python Cheat Sheet for classes
Python Cheat Sheet for Files & Exceptions
Python Cheat Sheet for Testing Code
Python Cheat Sheet for Pygame
Python Cheat Sheet for Matplotlib
Python Cheat Sheet for Pygal
Python Cheat Sheet for Django
Note: Advance Programming Tutorial Available on our YouTube Channel : Techprofree . Search on YouTube . Subscribe Our Channel.
Basic Concepts
Integers | 1,2,3,4,5,6,76, |
Floating-point numbers | 2.4 , 2.3, 4.5, 4.5 , |
String | ‘abc’ , ‘ads’ , ‘aaa’ |
Basic Math Operator used in Python
+ | Addition |
– | Subtraction |
* | Multiplication |
/ | Division |
// | Integer division |
** | Exponent |
Examples of Operators
Addition
2+3=5
Subtraction
3-2=1
Multiplication
2*2=4
Division
4/2=2
Integer Division
22//8 =2
Exponent
2**3=8
Best Cheat Sheet For Python
Comparison Operations
>= | Greater than or Equal to |
<= | Less than or Equal to |
> | Greater Than |
< | Less than |
!= | Not equal to |
Equal to |
Download Full Python Cheat-sheet
Best Python Cheat Sheet Pdf
Virus note:
Best Python Cheat Sheet
- All files are scanned by Team of Techringe.com for viruses
- Kindly Never run .exe’s, .ocx’s, .dll’s etc
- Only Run PDF, Word