From 211b2a67491b21391ba50a20409162ba110bca5b Mon Sep 17 00:00:00 2001 From: kyanda Date: Sun, 3 Nov 2019 09:34:34 +0300 Subject: [PATCH] python practice sets --- __pycache__/classes.cpython-37.pyc | Bin 0 -> 763 bytes __pycache__/datetime.cpython-37.pyc | Bin 0 -> 208 bytes areacalc.py | 10 ++++++ classes.py | 21 +++++++++++ comprehension.py | 14 ++++++++ controlflow.py | 22 ++++++++++++ date.py | 54 ++++++++++++++++++++++++++++ decorators.py | 24 +++++++++++++ functions.py | 6 ++++ loops.py | 14 ++++++++ modules.py | 19 ++++++++++ projects/dl_img.py | 16 +++++++++ stringsformat.py | 9 +++++ 13 files changed, 209 insertions(+) create mode 100644 __pycache__/classes.cpython-37.pyc create mode 100644 __pycache__/datetime.cpython-37.pyc create mode 100644 areacalc.py create mode 100644 classes.py create mode 100644 comprehension.py create mode 100644 controlflow.py create mode 100644 date.py create mode 100644 decorators.py create mode 100644 functions.py create mode 100644 loops.py create mode 100644 modules.py create mode 100644 projects/dl_img.py create mode 100644 stringsformat.py diff --git a/__pycache__/classes.cpython-37.pyc b/__pycache__/classes.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..7d84f435aa3dac3a1ba8833e00354a3cb070ff0d GIT binary patch literal 763 zcmZ`%&1%~~5T0HASaIz(B_XFCbQPg~08>h7DY^NQn{Zeb?UpE!C3RP(*y!fGMJcq$ zyhER4ue~&{kW*%)*bae=n4eF(`^`5ieIAd8fL2WYUici~AAzm;SbRdaDTXsppx6!i zhNqxGapft0!L2ImDU+)C8JCXU(oFdhL*J;pye1@>tW;y@GP4p zp7Tq&&@QyHkY?o~wKTHHt1|JOZ*}p_5RvgH5bZ6P5YzqX*>rxrlBJUKdevN3VfGQTcxy|Bn8&^_#7Pn0Tl0Iy&#JAFE>$6?dXz s*~C98b3Kh`$)KY$V?6DG+T>;P?8CgM%0(agVLo=!sQ>F%BX-z54=G=v^Z)<= literal 0 HcmV?d00001 diff --git a/__pycache__/datetime.cpython-37.pyc b/__pycache__/datetime.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..09daa9855e9dbb07e0e780fa943be9c949c1ac68 GIT binary patch literal 208 zcmZ?b<>g`k0{Mh}u^)i+V-N=hSb+=&ATCw{5-AKRj5!QZ45%Gcp3@f*CYfUjo%J_+>t3XJB~A4P?G#1`?Vqw>VM~OHxZRb5n1zfLON}Q*N;q z6lLa>tYj$S04f0!zjWNKVzMg}^HLIH3Mxx7^7EjSZb4CEa!F=#Y7ER6y@JYH95%W6 TDWy57b|6<4vj7PWCO$?06reN$ literal 0 HcmV?d00001 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