<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_speakers = 1
type(num_of_speakers)
print('I just bought 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_speakers > 1)<jupyter_output>False<jupyter_text>Code blocks - If, For, While<jupyter_code>if is_android == True:
  print(name + ' supports Android')
  print('Continuing to 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 numbers 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, 2)
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[2]
type(my_phone[0])
my_phone[1:3]
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_sim', 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)
print(my_phone)
type(my_phone)<jupyter_output>['iPhone 5s', False, 4.3, '3G', 'single_sim', 35000]<jupyter_text>Tuples, Sets, and Dicts<jupyter_code>my_phone_tuple = ('iPhone 5s', False, 4.3)
print(my_phone_tuple)
type(my_phone_tuple)
my_phone_tuple[0:2]
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 is to be measured
toc = time.time()
print('Elapsed 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('Elapsed time in seconds', toc - tic)
my_tuple = tuple(range(1000000))
tic = time.time()
my_tuple_squared = map(lambda i: i**2, my_tuple)
toc = time.time()
print('Elapsed time in seconds', toc - tic)
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(98731 in my_list)
print(131591 in my_list)
print(1111111111 in my_list)
toc = time.time()
print('Elapsed time', toc - tic)
my_set = set(range(1000000))
tic = time.time()
print(98731 in my_set)
print(131591 in my_set)
print(1111111111 in my_set)
toc = time.time()
print('Elapsed time', toc - tic)
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>name : iPhone 5s
isAndroid : False
screenSize : 4.3<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 secs with own function', toc - tic)

tic = time.time()
for x in vals:
  y = math.factorial(x)
toc = time.time()
print('Elapsed time in secs with own function', toc - tic)
!echo 'def hello():' > 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 https://www.dropbox.com/s/w94odi4aq1k44lf/mobile_cleaned.csv
!ls
file = open('mobile_cleaned.csv', 'r')
s = file.readline()
print(s)
print(s.split(','))
file.close()
file.readline()
with open('mobile_cleaned.csv', 'r') as file:
  print(file.readline())
with open('mobile_cleaned.csv', 'r') as file:
  print(file.read())
with open('mobile_cleaned.csv', 'r') as file:
  for line in file:
    for word in line.split(','):
      print(word)
    print('-------')
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_rating(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.screen_size = 4
new_phone.has_rating()
new_phone.rating = 3.9
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>NumPy<jupyter_code>import numpy as np
X = np.array([1, 2, 3])
print(X)
type(X)
l = [4, 5, 10]
X = np.asarray(l)
type(X)
print(X)
X = np.asarray(l, float)
print(X)
X.shape
np.array?
X = np.array([[1, 2, 3], [4, 5, 6]])
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)
p2 = np.outer(u, v)
print(p2)
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)
A.sum(axis = 1)
A.max()<jupyter_output><empty_output><jupyter_text>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()
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()
!wget https://www.dropbox.com/s/w94odi4aq1k44lf/mobile_cleaned.csv
!ls
import pandas as pd
data = pd.read_csv('mobile_cleaned.csv')
data.head()
ax = sns.scatterplot(x="stand_by_time", y="battery_capacity", data=data)
ax = sns.scatterplot(x = "stand_by_time", y = "battery_capacity", hue="thickness", data=data)
ax = sns.distplot(data["stand_by_time"])
ax = sns.distplot(data["stand_by_time"], kde=False, rug=True, bins = 20)
ax = sns.boxplot(x="is_liked", y="battery_capacity", data=data)
ax = sns.boxplot(x = "expandable_memory", y = "price", data=data)
uniform_data = np.random.rand(10, 12)
print(uniform_data)
ax = sns.heatmap(uniform_data, cmap="YlGnBu")
!wget https://s3.amazonaws.com/thinkific-import/153034/3f9yaVLKTyUGmlBPcSOA_logo_long.png
!ls
!mv 3f9yaVLKTyUGmlBPcSOA_logo_long.png onefourthlabs.png
!ls
import matplotlib.image as mpimg
img = mpimg.imread('onefourthlabs.png')
print(img)
imgplot = plt.imshow(img)<jupyter_output><empty_output><jupyter_text>Pandas<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15630885122410546, 'fraction_numerical': 0.0330743879472693, 'mean_word_length': 5.442001516300228, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 44, 'https://': 3, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '405644', 'license': 'MIT', 'n_tokens_mistral': 3781, 'n_tokens_neox': 3555, 'n_words': 828, 'path': 'Python Basics/PythonBasics.ipynb', 'repo_name': 'anuraglahon16/-A-Hands-On-Course-on-Deep-Learning', 'size': '8499'}