Python Cheat Sheet Basics

Python-Cheat-Sheet-Basics

Python Basics Cheat Sheet

Are you finding troublesome in remembering all the syntax that you have to work with Python for Data Science?

Guys, don’t stress in the event that you are a beginner and have no idea about how Python works, this Python for Data Science cheat sheet will give you a brisk reference of the basics that you must know to begin.

While at Intellipaat, we support our learners with a handy reference, that the reason we have created this Cheat sheet. Take a profound dive to learn Python by our online tutorial.

Python Basic:

In Python, every value has a datatype. in Python programming, everything is an article, data types are classes and variables are instance that means object of these classes.

There are various data types in Python. Some of the important types are listed below.

    • Numbers: a=2(Integer), b=2.0(Float), c=1+2j(Complex)

 

    • List: a=[1,2,3,’Word’]

 

    • Tuple: a= (1,2,4)

 

    • String: a=“New String”

 

    • Sets: a= {2,3,4,5}

 

    • Dictionary: x= {‘a’: [1,2],‘b’: [4,6]

Operators

In python, Operators are only the constructs which can manipulate the value of operands. For example, in the expression 5 + 10 = 15. Here, 5 and 10 are operands and + is operator.

    • Numeric Operator (Say, a holds 5, b holds 10)
        • + b = 15
        •  b = -5
        • * b = 50
        • b/a = 2
        • % a = 0
        • a**b =510
        • 0//2.0 = 3.0, -11//3 = -4
    • Comparison Operator
        • (a == b): not true
        • (a!= b): true
        • (a > b): not true.

       

        • (a > b): not true
        • (a >= b): not true
        • (a <= b) is true
    • Boolean Operator
        • and b
        • or b
        • not a

Operations

Python provides some of the built-in operations on various data types.

    • List Operations
        • List=[]:Defines an empty list

       

        • list[i]=a: Stores a at the ith position

       

        • list[i]: Retrieves the character at the ith position

       

        • list[i:j]: Retrieves characters in the range i to j

       

        • append(val): Adds item at the end

       

        • pop([i]): Removes and returns item at index i

       

    • String Operations
        • String[i]: Retrieves the character at the ith position

       

        • String[i:j]: Retrieves characters in the rangei to j

       

       

 

    • Dictionary Operations
        • dict={} : Defines an empty dictionary

       

        • dict[i]=a: stores “a” to the key “i

       

        • dict[i]: Retrieves the item with the key “i

       

        • key: Gives all the key items

       

        • values: Gives all the values

       

Flow Control Method:

Python programming language provides various looping and control statement that allow for more complicated execution paths.A loop statement allows us to execute a statement or group of statements multiple times.

    • if-else (Conditional Statement):

 

if price>=700: print(“Buy.”) else: print(“Don’t buy.”)

 

    • For loop (Iterative Loop Statement):

 

 

a=“New Text” count=0 for i in a: if i==‘e’: count=count+1 print(count)

 

    • While loop (Conditional Loop Statement):

 

 

a=0 i=1 while i<10: a=a*2 i=i+1 print(a)

 

    • Loop Control: Break, Pass and continue

Functions

A function is a block of code which only runs when it is called. We can pass data (parameters) into a function and after executing a function, it will return data as a result.

def new_function():
print(“Hello World”)
new_function()

 

Lambda Function

A lambda function is a small anonymous function. It can take any number of arguments but can only have one expression.

lambda a,b: a+b
lambda a,b: a*b

Generic Operations

In python, we have a huge a list of Python built-in functions. Some of them are:

    • range(5): 0,1,2,3,4

 

    • S=input(“Enter:”)

 

    • Len(a): Gives item count in a

 

    • min(a): Gives minimum value in a

 

    • max(a): Gives minimum value in a

 

    • sum(a): Adds up items of an iterable and returns sum

 

    • sorted(a): Sorted list copy of a

 

    • importing modules:  import random

 

File Operations:

In python, we have several functions for creating, reading, updating, and deleting files. Theopen() function takes two parameters – filename, and mode.

There are four unique methods (modes) for opening a record:

    • “r” – Read – Default value. Opens a file for reading, error if the file does not exist

 

    • “a” – Append – Opens a file for appending, creates the file if it does not exist

 

    • “w” – Write – Opens a file for writing, creates the file if it does not exist

 

    • “x” – Create – Creates the specified file, returns an error if the file exists

 

f= open(“File Name”,“opening mode”)

(Opening modes: r: read, w: write, a: append, r+: both read and write)

Try & Except Block:

The try block allow us to test a block of code for errors.The except block allow us to handle the error.

try:
[Statement body block]
raise Exception()
except Exception as e:
[Error processing block]

Oops Concepts:

Python is an item arranged programming language. In Python, almost everything is an article and has its own properties and methods. Here a class resembles an article constructor, or a “blueprint” for creating objects.

    • Inheritance: A process of using details from a new class without modifying existing class.

 

    • Polymorphism: A concept of using common operation in different ways for different data input.

 

    • Encapsulation: Hiding the private details of a class from other objects.

 

Class/object Example:

Class: class Pen:
pass
object:obj=Pen()

 

Comments:

Single line Comments in Python start with the hash character, # and multiline comment should be used with “””triple quotes”””

# Single Line Comment
"""
Multi-line comment
"""