bing
Flat 10% & upto 50% off + 10% Cashback + Free additional Courses. Hurry up
×
UPTO
50%
OFF!
Intellipaat
Intellipaat
  • Live Instructor-led Classes
  • Expert Education
  • 24*7 Support
  • Flexible Schedule

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

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 the reason we have created this Cheat sheet.

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

Download a Printable PDF of this Cheat SheetPython Basics Cheat Sheet

 

Python Basic:

Python is a high-level dynamic programming language which is very easy to learn. It comes with powerful typing and the codes are written in very ‘natural’ style, thats 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

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 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)
    • 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

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 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 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 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 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
“””

Download a Printable PDF of this Cheat Sheet

With this, we come to an end of Python Basics Cheat Sheet. To get in-depth knowledge, check out our interactive, live-online Intellipaat Python Data Science Certification Training here, that comes with 24*7 support to guide you throughout your learning period. Intellipaat’s Python course will let you master the concepts of widely-used and powerful programming language Python. You will gain hands-on experience in working with the various Python packages like SciPy, NumPy, 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.

Previous Next

Download Interview Questions asked by top MNCs in 2018?

Learn SQL in 16 hrs from experts



"0 Responses on Python Cheat Sheet Basics"

    Leave a Message

    100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.
    top

    Sales Offer

    Sign Up or Login to view the Free Python Cheat Sheet Basics.