<jupyter_start><jupyter_text>Simple data types and operators<jupyter_code>name='<NAME>'
type(name)
print(name)
screen_size = 4.3
type(screen_size)
num_of_spekers = 1
type(num_of_spekers)
print('I just borught an '+name)
a= 3
b =2 
print(a+b,a/b,a*b,a-b,a**b,a//b)
is_android = False
type(is_android)
print(is_android == True)
print(is_android == False and num_of_spekers >1)<jupyter_output>False<jupyter_text>Code blocks - If, For, While<jupyter_code>if is_android == True:
  print(name+ ' support Android')
  print('Containing tp print inside the if block')
print('Printing this outside the block')
  
for i in range(5):
  print(i,i**2)
range?
i = 0
while i<5:
  print(i,i**2)
  i += 1 #equivalent to i = i+1<jupyter_output>0 0
1 1
2 4
3 9
4 16<jupyter_text>Functions<jupyter_code>def print_squares(stop_val):
  for i in range(stop_val):
    print(i,i**2)
print_squares(5)
print_squares(3)
def sum_of_squares(stop_val):
  sum_ = 0
  for i in range(stop_val):
    sum_ +=i **2
  return sum_  
sum_of_squares(5)
sum_5 = sum_of_squares(5)
def print_squares_with_startval(stop_val,start_val=0):
  """Prints the squares of number starting from start_val (inclusive) and ending with stop_val (exclusive)"""
  for i in range(start_val,stop_val):
    print(i,i**2)
print_squares_with_startval(5)
print_squares_with_startval?<jupyter_output><empty_output><jupyter_text>Lists<jupyter_code>my_phone = [name, is_android,screen_size]
type(my_phone)
print(my_phone)
my_phone[0]
type(my_phone[0])
my_phone[0:2]
my_dads_phone = my_phone
print(my_dads_phone)
my_phone[2] = 4
print(my_phone)
print(my_dads_phone)
my_dads_phone = list(my_phone)
# my_dads_phone = my_phone[:]
my_phone[2] = 4.3
print(my_phone)
print(my_dads_phone)
len(my_phone)
len?
my_phone.append('3G')
print(my_phone)
len(my_phone)
iphone5s_some_prefs = ['single_size', 35000]
my_phone = my_phone + iphone5s_some_prefs
print(my_phone)
for data in my_phone:
  print(data)
range(5)
print(list(range(5)))
a = list(range(5))
print(a)
b = list(map(lambda x:x**2,a))
print(b)
c = list(filter(lambda x: x % 2 == 0,b))
print(c)<jupyter_output>[0, 4, 16]<jupyter_text>Tuples, Sets,Dictionary<jupyter_code>print(my_phone)
type(my_phone)<jupyter_output>['iPhone 5s', False, 4.3, '3G', 'single_size', 35000]<jupyter_text>Tuple<jupyter_code>my_phone_tuple = ('iPhone 5s', False, 4.3)
print(my_phone_tuple)
type(my_phone_tuple)
for data in my_phone_tuple:
  print(data)
my_phone_tuple[2] = 4
import time
tic = time.time()
print(tic)
time.time?
tic = time.time()
# do something that takes some time that os to be measured
toc = time.time()
print('Elaspse time in seconds', toc-tic)
my_list = list(range(1000000))
tic = time.time()
my_list_squared = map(lambda i: i**2, my_list)
toc = time.time()
print('Elaspse time in seconds', toc-tic)
my_tuple = list(range(1000000))
tic = time.time()
my_tuple_squared = map(lambda i: i**2, my_tuple)
toc = time.time()
print('Elaspse time in seconds', toc-tic)<jupyter_output>Elaspse time in seconds 0.01280522346496582<jupyter_text>Set<jupyter_code>my_set = set(my_phone)
print(my_set)
my_phone.append(4.3)
print(my_phone)
my_set = set(my_phone)
print(my_set)
my_list = list(range(1000000))
tic = time.time()
print(98733 in my_list)
print(131591 in my_list)
print(1111111111 in my_list)
toc = time.time()
print('Elaspse time in seconds', toc-tic)
my_set = set(range(1000000))
tic = time.time()
print(98733 in my_set)
print(131591 in my_set)
print(1111111111 in my_set)
toc = time.time()
print('Elaspse time in seconds', toc-tic)<jupyter_output>True
True
False
Elaspse time in seconds 0.0007152557373046875<jupyter_text>Dictionary<jupyter_code>my_phone_dict = {}
type(my_phone_dict)
my_phone_dict['name'] = 'iPhone 5s'
my_phone_dict['isAndroid'] = False
my_phone_dict['screenSize']=4.3
print(my_phone_dict)
my_phone_dict['name']
print(my_phone_dict.keys())
print(my_phone_dict.values())
print(my_phone_dict.items())
for key in my_phone_dict.keys():
  print(key,':', my_phone_dict[key])<jupyter_output><empty_output><jupyter_text>Python Packages<jupyter_code>import math
a = math.sqrt(100)
print(a)
a = math.pow(100,0.5)
print(a)
x = 100
y=1
for i in range(1, x):
  y *=i
print('Factorial of',x,'is',y)
y = math.factorial(x)
import math as m
y = m.factorial(x)
from math import factorial
y = factorial(x)
vals = list(range(1,100))
tic = time.time()
for x in vals:
  y=1
  for i in range(1,x):
    y *=i
toc=time.time()
print("Elapsed time in sec with on function", toc-tic)

tic = time.time()
for x in vals:
  y= math.factorial(x)
toc=time.time()
print("Elapsed time in sec with own function", toc-tic)<jupyter_output>Elapsed time in sec with on function 0.00078582763671875
Elapsed time in sec with own function 0.00018906593322753906<jupyter_text>Creating Own module in colab<jupyter_code>!echo 'def hello():' > my_first_module.py
!cat my_first_module.py
!echo '    print("hello, i am living in a different file!!!")' >> my_first_module.py
!cat my_first_module.py
import my_first_module
my_first_module.hello()
from my_first_module import hello
hello()<jupyter_output>hello, i am living in a different file!!!<jupyter_text>File Handling<jupyter_code>!wget http://insight.dev.schoolwires.com/HelpAssets/C2Assets/C2Files/C2ImportCalEventSample.csv
file = open('C2ImportCalEventSample.csv','r')
s = file.readline()
print(s)
print(s.split(','))
file.close()
file.readline()
with open('C2ImportCalEventSample.csv','r') as file:
  print(file.readline())
with open('C2ImportCalEventSample.csv','r') as file:
  print(file.read())
with open('C2ImportCalEventSample.csv','r') as file:
  for line in file:
    print(line)
    for word in line.split(','):
      print(word)
    print("----------Word Complete------")
with open('my_first_file_output.txt','w') as file:
  file.write('hello world from python code')
!cat my_first_file_output.txt<jupyter_output>hello world from python code<jupyter_text>Python Classes<jupyter_code>class MobilePhone:
  """This is a sample class to illustrate how Python classes work"""
  def __init__(self,name, is_android=False,screen_size=4.3):
    self.name = name
    self.is_android = is_android
    self.screen_size = screen_size
    self.rating = -1
  
  def has_ratings(self):
    return self.rating > -1
new_phone = MobilePhone('iPhone 5s')
type(new_phone)
print(new_phone.name,new_phone.is_android,new_phone.screen_size)
new_phone.has_ratings()
new_phone.rating = 3.9
new_phone.has_ratings()
MobilePhone.__doc__
class iPhone(MobilePhone):
  def __init__(self,name):
    MobilePhone.__init__(self,name,False,4)
  
  def __str__(self):
    return self.name+" "+str(self.is_android) + " "+ str(self.screen_size)
new_iphone = iPhone('iPhone 5s')
new_iphone.is_android
print(new_iphone)<jupyter_output>iPhone 5s False 4<jupyter_text>Basic NumPy<jupyter_code>import numpy as np
X = np.array([1,2,3])
print(X)
type(X)
l = [4,5,10]
X = np.array(l)
type(X)
print(X)
X = np.asarray(l,float)
print(X)
X.shape
np.array?
X = np.array([[1,2,4],[6,4,10]])
print(X)
X.shape
X[1,2]
X[0,0:2]
X[0,:]
X = np.zeros((4,5))
print(X)
X = np.eye(4,4)
print(X)
X = np.random.random((4,5))
print(X)
Z = X.T
print(Z)
Y = X.reshape(20,1)
print(Y)
A = np.arange(5)
print(A)
B = np.arange(5)
C = A+B
D = A-B
E = A*B
print(C,D,E)
print(A,A+1)
A = np.random.random((2,3))
print(A)
print(A+1)
A = np.floor(np.random.random((2,3))*10)
print(A)
u = [1,2,3]
v = [-1,0,1]
p1 = np.inner(u,v)
print(p1) 
"""(1*-1)+(2*0)+(3*1)"""
p2 = np.outer(u,v)
print(p2)
'''[u[1](v)
    u[2](v)
    u[3](v)]'''
A = np.ones((2,3))
B = np.ones((3,2))
np.dot(A,B)
np.dot(B.T,A.T)
A = np.ones((2,3))
print(A)
A.sum()
A.sum(axis = 0) #0=column
A.sum(axis=1) #1=row
A.max()<jupyter_output><empty_output><jupyter_text>Basic Plotting<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
x = np.random.random((10,1))
print(x)
plt.plot(x)
plt.show()
plt.plot(x,'*')
plt.show()
plt.plot(x,'*-')
plt.show()
x = np.linspace(0,10,100)
y = np.power(x,0.5)
plt.plot(x,y,'.')
plt.show()
x = np.linspace(0,100,50)
y = np.power(x,2)
plt.plot(x,y,'*')
plt.show()
import seaborn as sns
sns.set()
sns.lineplot(x,y)
plt.show()
!pip install seaborn==0.9.0
!wget http://www.superdatascience.com/wp-content/uploads/2015/06/OfficeSupplies.csv
#https://s3.amazonaws.com/thinkific-import/153034/mobile_cleaned-1549119762886.csv
!ls
import pandas as pd
data = pd.read_csv('OfficeSupplies.csv')
data.head()
ax = sns.scatterplot(x='Units', y ='Unit Price',data=data)
ax = sns.scatterplot(x='Units', y ='Unit Price',hue='Item',data=data)
ax = sns.distplot(data['Units'])
ax = sns.distplot(data['Units'], kde=False,rug=True,bins=20)
ax = sns.boxplot(x='Region', y ='Unit Price',data=data)
ax = sns.boxplot(x='Item', y ='Unit Price',data=data)
uniform_data = np.random.rand(10,12)
print(uniform_data)
ax = sns.heatmap(uniform_data,cmap="YlGnBu")
!wget https://www.gettyimages.com/gi-resources/images/500px/983794168.jpg
!ls
!mv 983794168.jpg firstimage.jpg
!ls
import matplotlib.image as mpimg
img = mpimg.imread('firstimage.jpg')
print(img)
imgplot = plt.imshow(img)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16217714222370025, 'fraction_numerical': 0.045338654251191664, 'mean_word_length': 6.229166666666667, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 53, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '653195', 'license': 'Apache-2.0', 'n_tokens_mistral': 4074, 'n_tokens_neox': 3812, 'n_words': 761, 'path': '0302_PythonBasicDataTypes.ipynb', 'repo_name': 'yuvaraj119/Colab-Notebooks', 'size': '9024'}