0% found this document useful (0 votes)
55 views7 pages

Python For Data Science AI Development

This document provides an overview of Python for data science, AI, and development. It covers Python basics like types, expressions, strings, lists, dictionaries, sets, conditions, loops, functions, classes and objects. It also discusses reading and writing files, loading data with Pandas, NumPy arrays, web scraping, and a Python cheat sheet.

Uploaded by

code.ayechan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views7 pages

Python For Data Science AI Development

This document provides an overview of Python for data science, AI, and development. It covers Python basics like types, expressions, strings, lists, dictionaries, sets, conditions, loops, functions, classes and objects. It also discusses reading and writing files, loading data with Pandas, NumPy arrays, web scraping, and a Python cheat sheet.

Uploaded by

code.ayechan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

🐍

Python for Data Science,


AI & Development
Types
Python Types

int for integer

float for real number

str for words

bool (True or False) for boolean

type(1) # give a type of data held by a variable

type casting → converting the existing type of data into another


one

Expressions and Variables


expressions → mathematical operations

1, 1.5, etc # operands


+, -, *, /, etc # operators

variables → to store a value

String Operations
positive index → to access element from start to end

string[0] # first element of the string


string[1]

Python for Data Science, AI & Development 1


...
string[n] # last element of the string

negative index → to access element from end to start

string[-1] # last element of the string


string[-2]
...
string[-(n+1)] # first element of the string

slicing → to access a subset of string

string[0:4] # elements from 0th index to 3rd index

stride

string[::2] # every second variable of tbe string

length

len(string)

replication

string = "hello "


3 * string # "hello hello hello "

string is immutable

escape sequence

\n # new line
\t # tab
\\ # just backslash

Python for Data Science, AI & Development 2


string.find("words") # find substring of a string

List and Tuples


list and tuples are compound datatypes and reference types
tuples are immutable while list are mutable

tuple = (1, 2, 3, 4) # tuple


tuple[0] = 1
tuple = tuple + (5, 6) # concatenate tuple
tuple[0:3] # (1, 2, 3)
len(tuple)

list = [1, 2, 3, 4]
list.extend([5, 6]) # [1, 2, 3, 4, 5, 6]
list.append(5) # [1, 2, 3, 4, 5]

Dictionaries
dictionary has key and value
keys are immutable and unique

dict = {}
dict.keys() # keys
dict.values() # values
dict["key"] # some value
"key" in dict # give `True` or `False`

Sets
set holds unique elements

set = {1, 2, 3, 4, 5}
set.add(6) # {1, 2, 3, 4, 5, 6}

Python for Data Science, AI & Development 3


10 in set # False
set1 & set2 # interception
set1.union(set2) # union
set1.issubset(set2)

Conditions and Branching

a == b # is equal?
a < 6
a != b # is not equal?

Loops

range(start, end) # start..<end


enumerate(list) # produce index and value

Functions

len(list) # return lenght of list


sum(list) # add all elements of list
sorted(list) # return sorted new list
sort(list) # sort list

Exception Handling

try:
...
...
except:
... # handle error
else:
... # task is done successfully

Python for Data Science, AI & Development 4


finally:
... # run no matter the end result

Objects and Classes


each object has:

type

internal data representation (blueprint)

methods

object → instance of a particular type

class Circle(object):
def __init__(self, color, radius):
self.radius = radius
self.color = color

def add_radius(self, r):


self.radius = self.radius + r

class Rectangle(object):
def __init__(self, color, height, width):
self.height = height
self.width = width
self.color = color

redCircle = Circle(10, "red")

dir(name_of_object) # return a list of object's attributes

Reading Files with Open

file = open(<file path>, <mode>) # open file (mode - r (reading)


file.name # name of file

Python for Data Science, AI & Development 5


file.mode # mode of file
file.close() # explicit close of file

with open(<file path>, <mode>) as file:


file_data = file.read()
print(file.closed) # True (already closed file)

file.readlines() # read file line by line

Writing Files with Open

with open(<file path>, <mode>) as file:


file.write("some text")

Loading Data with Pandas

import pandas as pd
df = pd.read_csv(<path>) # load dataframes
df.head() # give header of csv

One Dimensional Numpy

import numpy as np
a = np.array([0, 1, 2, 3, 4]) # create a numpy array
a.size # 5
a.ndim # 1 (number of dimension)
a[1:4] # [1, 2, 3]

Webscraping
webscraping → process that can be used to extract information
from a website and cen easily be accomplished within a matter of
minutes

Python for Data Science, AI & Development 6


Python Cheat Sheet

https://drive.google.com/file/d/1ksdgaz4rurbd0C_uVyFEPozJe4
ISWh4q/view?usp=drive_link

Python for Data Science, AI & Development 7

You might also like