-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 211b2a6
Showing
13 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#standard input | ||
# name = input('Enter your first name:') | ||
# print ('Hello' + name) | ||
|
||
#radius and area are examples of variables | ||
|
||
#Area of a circle | ||
radius = int(input('Enter radius:')) | ||
area = 3.142 * radius ** 2 | ||
print(f'The area of the circle is {area}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Employee: | ||
|
||
def __init__(self, name, salary, duration): | ||
self.name = name | ||
self.salary = salary | ||
self.duration = duration | ||
|
||
|
||
def time_spent(self): | ||
return self.salary * self.duration | ||
|
||
kyanda = Employee('kyanda', 1000, 10) | ||
print(f'Employee name: {kyanda.name}') | ||
print(f'Salary: {kyanda.salary}') | ||
print(f'time spent at work: {kyanda.duration}') | ||
print(kyanda.time_spent()) | ||
|
||
# instance level attributes | ||
# class level attributes | ||
# @classmethods | ||
# @staticmethods |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# list comprehension | ||
|
||
#double prize money weekend bonanza | ||
prizes = [5, 10, 50, 100, 1000] | ||
|
||
#normal way | ||
dbl_prizes = [] | ||
for prize in prizes: | ||
dbl_prizes.append(prize * 2) | ||
print(dbl_prizes) | ||
|
||
#list comprehension way | ||
dbl_prizes = [ prize * 2 for prize in prizes ] | ||
print(dbl_prizes) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
age = int(input('How old are you?')) | ||
|
||
if age < 10: | ||
print('You are young!') | ||
|
||
elif age <25: | ||
print('You are a youth!') | ||
|
||
else: | ||
print('gundi okuze!!!') | ||
|
||
#operators > < => =< | ||
|
||
#comparison operators == != | ||
|
||
menu = input('do you need a menu? (y/n)') | ||
|
||
if menu == 'y': | ||
print('here is the menu and you get a free soda!') | ||
|
||
else: | ||
print('save money and come back!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#dates, times, datetimes, timezones, timedeltas | ||
#naive date times - doesnt include time zone and day light savings | ||
#aware date time - includes time zones and day light savings | ||
|
||
import datetime | ||
|
||
d = datetime.date(2019, 11, 3) | ||
print(d) | ||
|
||
currentDate = datetime.date.today() | ||
print(currentDate) | ||
print(currentDate.year) | ||
|
||
#timedelta - difference between two dates and time | ||
#adding gets future date whereas subtracting gets past date | ||
tdelta = datetime.timedelta(days=30) | ||
print(f'Next date for paying subscription is: {currentDate + tdelta}') | ||
|
||
#TIME | ||
t = datetime.time(10, 30, 10, 10000) | ||
print(t) | ||
|
||
|
||
#DATETIME | ||
dt = datetime.datetime(2019, 11, 3, 10, 30, 10, 10000) | ||
tdelta2 = datetime.timedelta(days=30) | ||
print(dt) | ||
#calculate date difference with time | ||
print(dt + tdelta2) | ||
|
||
dt_today = datetime.datetime.today() | ||
print(dt_today) # prints current datetime with no timezone | ||
|
||
|
||
# thou it is recommended to use the PYTZ LIBRARY IF YOU ARE DEALING WITH TIMEZONE | ||
#pip install pytz | ||
|
||
import pytz | ||
dt_tz = datetime.datetime(2019, 11, 4, 8, 53, 4, tzinfo=pytz.UTC) | ||
print(dt_tz) | ||
|
||
dt_utcnow = datetime.datetime.now(tz=pytz.UTC) | ||
print(dt_utcnow) | ||
|
||
# converting this timezone to another | ||
dt_kampala = dt_utcnow.astimezone(pytz.timezone('Africa/Kampala')) | ||
print(f'Kampala timezone is: {dt_kampala}') | ||
|
||
# format code ---- python datetime documentation | ||
print(dt_kampala.strftime('%B %d, %Y')) | ||
|
||
# list of timezones | ||
# for tz in pytz.all_timezones: | ||
# print(tz) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#used a lot in django @login_required @permission_required | ||
|
||
def permission_dec(func): | ||
|
||
def func_wrapper(): | ||
#code before function | ||
print('please login to see secrets and list of presidents') | ||
func() | ||
#code after function | ||
print('you are logged in and permitted') | ||
|
||
return func_wrapper | ||
|
||
@permission_dec | ||
def secrets(): | ||
print('here are Ugandan secrets since 1900') | ||
|
||
secrets() | ||
|
||
@permission_dec | ||
def view_ugandan_presidents(): | ||
print("Uganda's presidents: Museveni Yoweri, Ignatius Musaazi, Tito Okello, Obote Milton ...") | ||
|
||
view_ugandan_presidents() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
def greet(): | ||
print("hello there!") | ||
|
||
greet() | ||
|
||
# return keyword |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#for loop | ||
fruits = ['apple', 'pineapple', 'water melon', 'mango', 'straw berry'] | ||
|
||
for fruit in fruits: | ||
print(fruit) | ||
|
||
|
||
for fruit in fruits[0:2]: | ||
print(fruit) | ||
|
||
#useful in django for outputting content | ||
# break keyword | ||
#while loop | ||
# continue keyword |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# modules and packages | ||
# modules - pieces of code split into logical sections. these python files can be imported and reused | ||
# packages - a collection of modules | ||
|
||
#Example: | ||
from classes import Employee | ||
|
||
chiya = Employee('chiya', 2000, 5) | ||
print(f'Employee name: {chiya.name}') | ||
print(f'Salary: {chiya.salary}') | ||
print(f'time spent at work: {chiya.duration}') | ||
print(chiya.time_spent()) | ||
|
||
|
||
#Package | ||
#setup: | ||
# create folder | ||
# add __init__.py file | ||
#add other py files in folder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import urllib.request | ||
|
||
def dl_jpg(url, file_path, file_name): | ||
full_path = file_path + file_name + '.jpg' | ||
urllib.request.urlretrieve(url, full_path) | ||
|
||
url = input('Enter image url: ') | ||
file_name = input('Enter file name to save as: ') | ||
dl_jpg(url, 'images/', file_name) | ||
|
||
|
||
#download images like in the social network movie | ||
#face mash | ||
|
||
#urllib documentation | ||
#video downloads eg from youtube |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
num1= 3.142 | ||
num2= 10.234567 | ||
|
||
#previously areacalc | ||
#print('num1 is:', num1, 'num2 is:', num2) | ||
|
||
|
||
# Format method | ||
print('num1 is {0} and num2 is {1}'.format(num1,num2)) |