Jul 13, 2024
Instructor: Mosh
app.py
.app.py
:
print("Hi, I am Mosh Hamedani")
print("O----")
print(" ||||")
print("*****")
print("*" * 10)
price = 10
rating = 4.9
name = "Mosh"
is_published = True
input()
function:
name = input('What is your name? ')
print('Hi ' + name)
color = input('What is your favorite color? ')
print(name + ' likes ' + color)
int()
float()
bool()
birth_year = int(input('Enter your birth year: '))
age = 2023 - birth_year
print('Your age is ' + str(age))
first_name = "John"
last_name = "Smith"
msg = f'{first_name} [{last_name}] is a coder'
print(msg)
len()
: Length of a stringupper()
: Convert to uppercaselower()
: Convert to lowercasefind()
: Return the index of the first occurrencereplace()
: Replace substringsin
operator: Check existencecourse = 'Python for Beginners'
print(course.upper())
print(course.find('y'))
print(course.replace('for', '4'))
print('Python' in course)
print(10 + 3)
print(10 - 3)
print(10 * 3)
print(10 / 3) # returns float
print(10 // 3) # returns int
print(10 % 3)
print(10 ** 3)
x = 10
x += 3
names = ['John', 'Bob', 'Mosh', 'Sarah', 'Mary']
names[0] = 'Jon'
print(names[0:3])
append()
: Add item to endinsert()
: Add item at specific indexremove()
: Remove itemclear()
: Clear listpop()
: Remove last itemindex()
: Find index of itemnumbers = [1, 2, 3, 4, 5]
numbers.append(6)
numbers.insert(0, -1)
numbers.remove(3)
numbers.clear()
numbers = [1, 2, 3, 4, 5]
print(numbers.index(3))
numbers = (1, 2, 3)
print(numbers[0])
def greet_user():
print('Hi there!')
print('Welcome aboard')
greet_user()
def greet_user(name):
print(f'Hi {name}!')
greet_user('John')
def greet_user(first_name, last_name):
print(f'Hi {first_name} {last_name}!')
greet_user('John', last_name='Smith')
try:
age = int(input('Age: '))
print(age)
except ValueError:
print('Invalid Value')
try:
age = int(input('Age: '))
income = 20000
risk = income / age
print(age)
except ZeroDivisionError:
print('Age cannot be 0')
except ValueError:
print('Invalid Value')
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def move(self):
print('move')
def draw(self):
print('draw')
point = Point(10, 20)
point.draw()
class Mammal:
def walk(self):
print('walk')
class Dog(Mammal):
def bark(self):
print('bark')
dog1 = Dog()
dog1.walk()
dog1.bark()
import converters
from converters import kg_to_lbs
ecommerce/
__init__.py
shipping.py
from ecommerce.shipping import calc_shipping
import random
print(random.random())
print(random.randint(1, 10))
Path
from pathlib
module:
from pathlib import Path
path = Path("ecommerce")
print(path.exists())
pip install openpyxl
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
workbook = xl.load_workbook('transactions.xlsx')
sheet = workbook['Sheet1']
for row in range(2, sheet.max_row + 1):
cell = sheet.cell(row, 3)
corrected_price = cell.value * 0.9
corrected_price_cell = sheet.cell(row, 4)
corrected_price_cell.value = corrected_price
values = Reference(sheet, min_row=2, max_row=sheet.max_row, min_col=4, max_col=4)
chart = BarChart()
chart.add_data(values)
sheet.add_chart(chart, 'e2')
workbook.save('transactions2.xlsx')
import pandas as pd
df = pd.read_csv('music.csv')
X = df.drop(columns=['genre'])
y = df['genre']
from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier()
model.fit(X, y)
predictions = model.predict([[21, 1], [22, 0]])
print(predictions)
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
score = accuracy_score(y_test, predictions)
print(score)
import joblib
joblib.dump(model, 'music-recommender.joblib')
model = joblib.load('music-recommender.joblib')
predictions = model.predict([[21, 1]])
from sklearn import tree
tree.export_graphviz(model, out_file='music-recommender.dot',
feature_names=['age', 'gender'],
class_names=sorted(y.unique()),
label='all', rounded=True, filled=True)
pip install django==2.1
django-admin startproject pyshop .
```