Process Advisors

ey-logo
*Subject to Terms and Condition

Python Basics Cheat Sheet

Are you finding difficulty remembering all the syntax that you need to work with Python?
Guys, don’t worry if you are a beginner and have no idea about how Python works, this Python for Data Science cheat sheet will give you a quick reference of the basics that you must know to get started.
While at Intellipaat, we support our learners with a handy reference, that is the reason we have created this Cheat sheet. Take a deep dive to learn Python by our online tutorial.

Download a Printable PDF of this Cheat Sheet

Python Basics Cheat Sheet

This cheat sheet is designed for the one who has already started learning about Python but needs a handy reference sheet.

Watch Python Interview Questions tutorial:

Python Basic:

Python is a high-level dynamic programming language that is very easy to learn. It comes with powerful typing and the codes are written in a very ‘natural’ style, that’s the reason, it is easy to read and understand. Python programming language can run on any platform, from Windows to Linux to Macintosh, Solaris, etc.

Python Basic

 

 

 

 

Learn Python from basics to advance. Enroll in Python course in Bangalore!

Basic rules to write Python syntax:

Rule #1: Python is white-space dependent; code blocks are indented using spaces.
Rule #2: Python language is case sensitive. It matters for variables, functions and any keyword in general.

Data Types

In Python, every value has a datatype. in Python programming, everything is an object, data types are classes and variables are instances that mean the 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 that 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)
        • a + b = 15
        • a b = -5
        • a * b = 50
        • b/a = 2
        • b % 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
        • a and b
        • a or b
        • not a

Get 100% Hike!

Master Most in Demand Skills Now !

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 an 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 range i 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

       

Have a look at our blog on Python modules and frameworks to learn more!

Flow Control Method:

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

 

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)

 

 

 

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 that 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()

Career Transition

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 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 different methods (modes) for opening a file:

    • “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 allows us to test a block of code for errors. The except block allows us to handle the error.

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

Oops Concepts

Python is an object-oriented programming language. In Python, almost everything is an object and has its own properties and methods. Here a class is like an object constructor or a “blueprint” for creating objects.

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

 

    • Polymorphism: A concept of using common operations 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
"""

Download a Printable PDF of this Cheat Sheet

With this, we come to an end of the Python Basics Cheat Sheet. To get in-depth knowledge, Data Science with Python that comes with 24*7 support to guide you throughout your learning period. Intellipaat’s Python course will let you master the concepts of the widely-used and powerful programming language Python. You will gain hands-on experience in working with various Python packages like SciPy, NumPy, Python MatPlotLib, Lambda function, and more. You will work on hands-on projects in the domain of python and apply it for various domains of big data, data science, and machine learning.
A lot of questions can be raised to beginners on this topic in interviews. Check out the list of Python interview questions created by the experts.

Certification in Full Stack Web Development

Course Schedule

Name Date Details
Python Course 03 Jun 2023(Sat-Sun) Weekend Batch
View Details
Python Course 10 Jun 2023(Sat-Sun) Weekend Batch
View Details
Python Course 17 Jun 2023(Sat-Sun) Weekend Batch
View Details

2 thoughts on “Python Cheat Sheet Basics”

Leave a Reply

Your email address will not be published. Required fields are marked *