Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
suyashi29
GitHub Repository: suyashi29/python-su
Path: blob/master/Data Science using Python/Day1 Python for Data Science.ipynb
3074 views
Kernel: Python 3 (ipykernel)
  • obtaining knowledge from often enormously large data sets.

  • process include analysis, preparing data for analysis, and presenting results to support organisational decisions

Data Science is the combination of

  • statistics,

  • mathematics,

  • programming,

  • problem-solving,

  • capturing data in ingenious ways, the ability to look at things differently,

  • and the activity of cleansing, preparing and aligning the data.

image-3.png

Python For Data Science

  • Python is one of the most popular languages used by Data Scientists. Python has in-built mathematical libraries and functions, making it easier to calculate mathematical functions.

Data Containers in Python

  1. Strings 2. Lists 3. Tuples 4. Dictionary

Variable in Python

A=8 b=10
type(-2.0900000) type(2+6j)
complex

Operations on Numbers

2+2,2*2,2/2,3//2,3**3
(4, 4, 1.0, 1, 27)
x=2 #2x2+4x-9 2*(x*x)+4*x-9
7
##Variable Declaration a=9 A=5 A_1=9 a**2
81
#Quick Practice ## What is the output for (4x3+3x2+5x+5)?

Type Coversions

## Varibles declaration print(int(12.6)) print(float(3)) print("Hi how are you") print(2+3,4-6,2**3)
12 3.0 Hi how are you
print(2+3,4-6,2**3,sep=",")
5,-2,8
x=6 print("Total number of partcipants:",x)
Total number of partcipants: 6
Name = input("Name:") Age=input("Age") Name+Age type(Name)
Name:12 Age23
str
##Input Name=input("Name: ") Age=int(input("Age: ")) Weight=float(input("Height: "))
Name: Ashi Age: 23 Height: 12.4
## Link : https://jupyter.org/try-jupyter/lab/

Quick Pratice:

  1. Create python Script to input following details about your products:

  • Number of Products (30)

  • Name of Products Category (3*10)

  • Cost per Unit

  • Total Cost Price

solun a=int(input("no of product ")) b=input("Name of Products Category ") c=float(input("Cost per Unit ")) Total_CP=a*c print("Total Cost Price = ",Total_CP )

Strings

x="a" Y="ABCD" C="Data Science"
##Functions and methods in strings len(Y) len(C) Y[1] #Index Y[-3] Y[2:4] #Slicing ##Y[4]="E" #Immutable ##del Y[0] del Y
x="ashi29" a="82154" print(min(a),max(a)) print(min(x),max(x))
2 s
C="Data is important." C.split(" ")
['Data', 'is', 'important.']
name=input("Enter your Name :")
Enter your Name :Data
C+C #3- Concadination 3*C #- Repeat
'Data is important.Data is important.Data is important.'

Quick Practice:

Write a python script to open a railway ticket booking portal and enter following details from users :

  • 1-Name

  • 2-last Name

  • 3-Age

  • 4-Gender

  • 5-Boarding Point

  • 6-destination

## Solution

String Functions and Methods

x=" " x.isalpha() x.isspace()
True
z=x.encode() x.isalnum() x.isalpha() x.isascii() x.isdecimal() x.isspace() x.join("a")
x.isspace()
x.isalpha()
if (con): if(con): if(con): ---- else(*) else else

If else condition

a=int(input("Enter your Age:")) if a>=18: print("Welcome and sign in to this portal") name=input("UserName:") if len(name)>=5: pwd=input("Create Password:") else: print("User Name does not met the criteria") else: print("Opps! your age is not valid to use this portal")
Enter your Age:21 Welcome and sign in to this portal UserName:Ashirai Create Password:ashi1234

Quick Parctice:

  1. Write a python script to open a railway ticket booking portal and enter following details from users :

  2. Ask for Sign in with UserName, Password (Greater than 5), Age > 16

  3. Login for booking UserName and Pwd

1-Name 2-last Name 3-Age 4-Gender 5-Boarding Point 6-destination

## If else: Name= input("Enter UserName") Age=int(input("Age Please")) if len(Name) >=5: print("Valid UserName\t:") if Age>15: print("You can Proceed with booking") else: print("Your Booking Not allowed") else: print("Please check userName Criteria")
x=range(1,10) x
range(1, 10)
x=list(range(1,10,3) )#start,stop-1,interval x
[1, 4, 7]

Quick Practice:

  • Please create a user name of length 6 if user name is valid then create a password of length 7, if pwd is valid then print("Welcome to homepage")

  • Write a program that performs addition, subtraction, multiplication, or division based on user input. Prompt the user to enter two numbers and an operation (+, -, *, /). Perform the calculation and print the result.

## Please create a user name of length 6, create a password of lenghth 7 user=input("Create user name for minimum 6 lenghth:") if len(user) >= 6: print("Valid user name") else: print("Ops! User name not created invalid format")
## Loops for i in range(0,3): user=input("Enter your name:")
Enter your name:1 Enter your name:2 Enter your name:3
for i in "ash": user=input("Enter your name:\t")

Ques 2:

Input create a password with following critera:

  1. Min len 3 and 10

  2. Should contain one number

  3. Should contain upper case member

  4. Should not have a space

  5. Shoukd have one special charactor sp=["@","?","*","$","&","^",#.!]

## Atleast one number d=0 s=0 u=0 sp=["@","?","*","$"] user=input("Create user name for minimum 6 lenghth:") for i in user: if i.isdigit(): d=d+1 if i.isspace(): s=s+1 if i.isupper(): u=u+1 else: pass if d>0 and s==0: pass else: print("Opps !! you have not added a digit to username or included a space") if u>0: print("Valid User name") else: print("Opps !! you have not added a uppercase ")
Create user name for minimum 6 lenghth:ashi1234 Opps !! you have not added a uppercase
s="poor" #3s[4]=s #del s[1] del s s= "this is first session for data Science" #s.split(" ") s.index("f")

List

##a=[],[[]],[[[]]] a=[1,1,2] b=[1,2.1,"a"] c=[[1,12],[21,3]]
a.append(5) a del a[0]
a.insert(2,3)
a
[1, 2, 3, 9, 2, 4, 5, 5]
len(a) max(a) min(a)
1
## Tuples b=(1,2,3) #b.append(4) ##del b[2] ## tuples also does nit support item addition or deletion.
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [97], in <cell line: 5>() 2 b=(1,2,3) 3 #b.append(4) ----> 5 del b[2] TypeError: 'tuple' object doesn't support item deletion
del b

Dictionary

  • Mapping data types

  • Key value pairs

  • key are index to members for dictionary

  • Keys are unique with one value for one key

  • Duplicate Key creation is not possible

d={} d={"A":"red","B":"Blue"}
d.keys()
dict_keys(['A', 'B'])
d.values()
dict_values(['red', 'Blue'])
d["C"] ="green"
del d[C]
d
{'A': 'red', 'B': 'Blue', 'C': 'green'}
del d['C']
d
{'A': 'red', 'B': 'Blue'}
d={1:"a",2:"b"} d
{1: 'a', 2: 'b'}
d[1]="a" d
{1: 'a', 2: 'b'}
## User Input List x=[] n=int(input("Enter number of members" )) for i in range (0,n): m=int(input("Add list member\t:")) x.append(m) print(x)
Enter number of members2 Add list member :1 Add list member :2 [1, 2]
#### user input Diction d={} n=int(input("Enter number of members")) for i in range (0,n): a=input("Enter Key Name") d[a]=b b=input("Enter VALUE Name") print(d)
Enter number of members3 Enter Key NameA Enter VALUE NameB Enter Key NameC Enter VALUE Name1 Enter Key Name2 Enter VALUE Name3 {'A': 'c', 'C': 'B', '2': '1'}

Quick Practice 3 :

  • Create a list with all float values

  • Create a dictionary with Emp ID as Key and Names as Values

#Create a dict emp ID as key and names as values emp_data={} n=int(input("Enter number of employees")) for i in range (0,n): Emp_id=input("Enter Emp ID") Name = input("Enter Name") d[Emp_id]=Name print(d)
## Create a dic named Emp_details with Emp ID as Keys and job roels as values. "A101, B101"

Loops

for i in range (0,5): input("Enter Emp name\t: ")