Skip to content

Commit

Permalink
python practice sets
Browse files Browse the repository at this point in the history
  • Loading branch information
kyandaks committed Nov 3, 2019
0 parents commit 211b2a6
Show file tree
Hide file tree
Showing 13 changed files with 209 additions and 0 deletions.
Binary file added __pycache__/classes.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/datetime.cpython-37.pyc
Binary file not shown.
10 changes: 10 additions & 0 deletions areacalc.py
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}')
21 changes: 21 additions & 0 deletions classes.py
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
14 changes: 14 additions & 0 deletions comprehension.py
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)
22 changes: 22 additions & 0 deletions controlflow.py
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!')
54 changes: 54 additions & 0 deletions date.py
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)
24 changes: 24 additions & 0 deletions decorators.py
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()
6 changes: 6 additions & 0 deletions functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def greet():
print("hello there!")

greet()

# return keyword
14 changes: 14 additions & 0 deletions loops.py
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
19 changes: 19 additions & 0 deletions modules.py
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
16 changes: 16 additions & 0 deletions projects/dl_img.py
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
9 changes: 9 additions & 0 deletions stringsformat.py
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))

0 comments on commit 211b2a6

Please sign in to comment.