Python

Cheat sheet for python to get you started in 5 minutes

Cheat sheet for python

Cheatsheet has always been Kickstarter for a developer. Here is a cheat sheet for python to get you a quick start before you jump on to that project.

It is natural that you don’t remember everything at the same time. Cheatsheet helps a lot when you are working on a big and complicated project.

Basic operators

The table below deals with some basic operators to deal with in python.

a = 5 The value 5 will be assigned to the variable a
print(a) The print() function will print the value of a
type(a) The type() function will return the type of variable a (in this case the type of variable is an integer)
help(print) The help() function will show the documentation of print() function

[su_label type=”success”]Suggested read[/su_label] Convert list to string in python

Some other operators

a == 7 This will check whether a is equal to 7.
a != 7 This will check whether a is not equal to 7.
a > 7 This will check whether a is greater than 7.
a < 7 This will check whether a is less than 7.
a >= 7 This will check whether a is greater than or equal to 7.
a <= 7 This will check whether a is less than or equal to 7.
a == 7 or string == “python” Check whether a is equal to 7 or string is equal to python.
a == 7 and string == “python” Check whether a is equal to 7 and string is equal to python.
7 in list_a Check whether the list_a contains 7.
“GC” in dictionary_d Check whether the value corresponds to “GC‘ keys exist or not in the dictionary.

[su_label type=”success”]Suggested read[/su_label] Python string to int conversion tutorial

String cheatsheet

Some code snippets explaining how to use strings in python. This is one of the most important section of this Cheat sheet for python.

string_1 = “python is amazing” The string “python is amazing” will be stored in the string variable named string_1
string_1 = “”  every developer said, “ python is a great programming language.   ”” This method is used to create a string that contains symbols like “ and ‘. The multiline string will be stored in the variable named string_1.
len(string_1) The len() function will return the number of characters that are present in string_1.
string_1.startwith(“amazing”) The startwith() method checks whether the string starts with the given substring or not.
string_1.endswith(‘python’) The endswith() method checks whether the string ends with the given substring or not.
“{} plus {} is {}”.format(2,3,1) This method will return a string that contains values 2,3,1
string_1.replace(“a”,”b”) This method will return a new string. In the new string, every a will be replaced by b.
string_1.strip() This method will return a new string that does not contain and whitespaces at the start.
string_1.split(“ “) This method will return a list of string

[su_label type=”success”]Suggested read[/su_label] Learn how to open file in python

List cheatsheet

list_a = [5,7,8,e,r,f,g,h,4’] The list variable named list_a will contain a list that have the values 5,7,8,e,r,f,g,h,4.
list_a = list() The list() method will create an empty list.
list_a[0] This will return the value at index 0 in the list.
list_a[-1] This will return the last value from the list.
len(list_a) This method will return the number of elements that the list contains.
sum(list_a) This method will return the sum of all elements that the list contain
min(list_a) This method will return the minimum value from the list.
max(list_a) This method will return the maximum value from the list.
list_a.append(“python”) This method will add the string python at the end of the list.
list_a.clear() This method will clear the list.
list_b = list_a.copy() This method will copy the list_a into list_b.
list_a.count(element) This method will count the occurrence of a given element.
list_a.sort() This method will sort the list.
list_a.reverse() This method will be used to reverse a list.
list_a.remove(element) This method will remove the given element from the list.
list_a.pop(index) This method will pop and return the value of the element of the given index.
list_a.insert(index,item) This method will insert an element at the given address.
list_a.extend(list_b) This method will extend a list with another list.
list_a.index(item) This method is used to find the index of a given element.
“ ”.join([‘central_city’,’gotham_city’]) This method is used to convert a list into a single string like this “central_city gotham_city”.

Dictionary methods

dictionary_d = [“NW”: ”new_york”,”CC”: “central_city”, “GC”: “gotham_city” ] This will create a dictionary variable named dictionary_d that will contain the keys “NW”, “CC” and “GC”. and the values corresponding to the keys are “new_york”,”central_city” and “gotham_city”.
dictionary_d[“CC”] This will return the value from the dictionary named dictionary_d that has the key “CC”.
dictionary_d.get(“NW”,” can’t find the value”) This method will return the value associated with a key called “NW”. and if the key does not found then it will return “can’t find the value”.
dictionary_d.key() The key() method will return all the keys that are present in the dictionary.
dictionary_d.value() This method will return all the values that are present in the dictionary.
dictionary.items() This method will a dictionary in the form of a key-value pair.

[su_label type=”success”]Suggested read[/su_label] Python loops explained with examples

Loop Statements

If a > 7 :
    print(“a is greater than 7”)
elif a < 0:
    print(“a is less than 7”)
else:
    print(“the value of a lies between 0 and 7”)
In this method, the value of a will be checked every time and will run the print statements according to the value of a.
for item in list_a :
  print(item)
This method will print all the elements from the list_a.
while a < 7 :
  print(“python is awesome”)
  a += 1
This method will print the statement “python is awesome ” until or unless the value of a is less than 7.

[su_label type=”success”]Suggested read[/su_label] Sorting a list in python

Conclusion :

We have listed almost every basic and important concepts that are normally used in this Cheat sheet for python.

You don’t need to worry about remembering all this stuff because you already got a special cheatsheet that will help you to make a project without any trouble.

If you daily visit this cheatsheet then you will automatically able to remember all these basic and important functionalities in python.

Practice make learning easy so while making a program just have a look at this cheat sheet.

Related posts:

what is lambda in python
Python

What is lambda in python? Detailed explanation with examples

Everyone who wants to study programming must have heard that python is one of the easiest programming languages out there.
How to read a text file line by line in python
Python

How to read a text file line by line in python explained with examples

If you are looking for an explanation with examples on How to read a text file line by line in