diff --git a/__pycache__/classes.cpython-37.pyc b/__pycache__/classes.cpython-37.pyc new file mode 100644 index 0000000..7d84f43 Binary files /dev/null and b/__pycache__/classes.cpython-37.pyc differ diff --git a/__pycache__/datetime.cpython-37.pyc b/__pycache__/datetime.cpython-37.pyc new file mode 100644 index 0000000..09daa98 Binary files /dev/null and b/__pycache__/datetime.cpython-37.pyc differ diff --git a/areacalc.py b/areacalc.py new file mode 100644 index 0000000..98167c6 --- /dev/null +++ b/areacalc.py @@ -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}') \ No newline at end of file diff --git a/classes.py b/classes.py new file mode 100644 index 0000000..8be3673 --- /dev/null +++ b/classes.py @@ -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 \ No newline at end of file diff --git a/comprehension.py b/comprehension.py new file mode 100644 index 0000000..fdcbb52 --- /dev/null +++ b/comprehension.py @@ -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) \ No newline at end of file diff --git a/controlflow.py b/controlflow.py new file mode 100644 index 0000000..af77712 --- /dev/null +++ b/controlflow.py @@ -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!') \ No newline at end of file diff --git a/date.py b/date.py new file mode 100644 index 0000000..68e8e1d --- /dev/null +++ b/date.py @@ -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) diff --git a/decorators.py b/decorators.py new file mode 100644 index 0000000..f3886fa --- /dev/null +++ b/decorators.py @@ -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() \ No newline at end of file diff --git a/functions.py b/functions.py new file mode 100644 index 0000000..9856699 --- /dev/null +++ b/functions.py @@ -0,0 +1,6 @@ +def greet(): + print("hello there!") + +greet() + +# return keyword \ No newline at end of file diff --git a/loops.py b/loops.py new file mode 100644 index 0000000..84d2e3a --- /dev/null +++ b/loops.py @@ -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 \ No newline at end of file diff --git a/modules.py b/modules.py new file mode 100644 index 0000000..b947eb7 --- /dev/null +++ b/modules.py @@ -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 \ No newline at end of file diff --git a/projects/dl_img.py b/projects/dl_img.py new file mode 100644 index 0000000..e8a6bdb --- /dev/null +++ b/projects/dl_img.py @@ -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 \ No newline at end of file diff --git a/stringsformat.py b/stringsformat.py new file mode 100644 index 0000000..e1d2930 --- /dev/null +++ b/stringsformat.py @@ -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)) \ No newline at end of file