Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,303 members, 7,819,026 topics. Date: Monday, 06 May 2024 at 10:12 AM

Common good python programming practices you should know - Programming (6) - Nairaland

Nairaland Forum / Science/Technology / Programming / Common good python programming practices you should know (12725 Views)

A Thread For Tutorial On Python Programming / A Very Good Python Programmer Needed Asap / Recommend A Good Python Data Mining Book (2) (3) (4)

(1) (2) (3) (4) (5) (6) (7) (8) (Reply) (Go Down)

Re: Common good python programming practices you should know by iCode2: 10:30am On Jun 06, 2020
Should:
lmao
this is what I've been awake for...
with this pace, I'd probably be killing Artificial Intelligence(AI) by nxt week!.. grin grin
grin grin That's the spirit!!
Re: Common good python programming practices you should know by Nobody: 10:44am On Jun 06, 2020
crunchyDope:

Lol feminine language? this one loud and sounds true!

too easy!

Programming is not about learning a difficult or easy syntax, it is about knowing the fundamentals. Either you choose Python, Ruby, PHP, Java, C#, etc, can you do the sleekest thing with them?

Today, Python is the most sought-after programming language that let you worry less about the syntactic make-up and focus on the big events with its rich collections of libraries. Why choosing to do what you can do with just 5 lines of Python code with 30 lines of code of some other syntactically difficult languages? There are too many things to learn than wasting your time on syntax.

Moreover, I will still never agree that Python is as simple as many people try to believe, not when you have tried some advanced topics that will leave you sweating over your computer. One can only say that Python is simple if what one is doing all along is playing safe.

1 Like

Re: Common good python programming practices you should know by gbolly1151(m): 2:04pm On Jun 06, 2020
donproject:


Programming is not about learning a difficult or easy syntax, it is about knowing the fundamentals. Either you choose Python, Ruby, PHP, Java, C#, etc, can you do the sleekest thing with them?

Today, Python is the most sought-after programming language that let you worry less about the syntactic make-up and focus on the big events with its rich collections of libraries. Why choosing to do what you can do with just 5 lines of Python code with 30 lines of code of some other syntactically difficult languages? There are too many things to learn than wasting your time on syntax.

Moreover, I will still never agree that Python is as simple as many people try to believe, not when you have tried some advanced topics that will leave you sweating over your computer. One can only say that Python is simple if what one is doing all along is playing safe.
Well said....

>>>python get complex when someone begin to define wrapper with decorators and functional concept where functions in some cases chaining this way


My_new = func(argument)(first_fuc)(second_func)


>>>More complex,when writing abstract class using metaclass and
>>> most complex when writing in c language to work in python(most advanced)
Re: Common good python programming practices you should know by Nobody: 3:00pm On Jun 06, 2020
gbolly1151:

Well said....

>>>python get complex when someone begin to define wrapper with decorators and functional concept where functions in some cases chaining this way


My_new = func(argument)(first_fuc)(second_func)


>>>More complex,when writing abstract class using metaclass and
>>> most complex when writing in c language to work in python(most advanced)

Exactly, I can see you've taken your game further beyond comfort zone.

1 Like

Re: Common good python programming practices you should know by Predstan: 4:06pm On Jun 06, 2020
gbolly1151:


Storing rows using nested list would have been easy...right now,i haven't think what best algorithm will be better.

I decided to use Binary Search and Sorted List, now looks good a little.
Re: Common good python programming practices you should know by gbolly1151(m): 6:24pm On Jun 06, 2020
Predstan:


I decided to use Binary Search and Sorted List, now looks good a little.
Do you understand that your equation to calculate complexity will be

Sorted list + binary search
Re: Common good python programming practices you should know by gbolly1151(m): 6:29pm On Jun 06, 2020
donproject:


Exactly, I can see you've taken your game further beyond comfort zone.
One of my goals is to know in and out of python and along this path i can see c language being my next programming language because of cpython implementation and being the first language to implement python but dont think i can move to it now.
Re: Common good python programming practices you should know by gbolly1151(m): 7:57pm On Jun 06, 2020
What are factory functions?
These are functions that return inner function

def myprint():
return print
customized_print=myprint()
customized_print('hello word')

Output: hello world

#let use some print argument on our customized_print

customized_print('hello worl',end='.')


This is the basic of decorator in python
Re: Common good python programming practices you should know by Predstan: 9:14pm On Jun 06, 2020
gbolly1151:

Do you understand that your equation to calculate complexity will be

Sorted list + binary search

I used the Binary search to insert an element into the right position in the list. So the 0(.) Is just Logn. Instead of returning true or false, it returns the position of an item after divide and conquer, then I use the insert function to insert in the right position in the list.

I also wrote a game called Game of Life where organisms dies or lives with respect to the number of organisms around them that are alive. Its similar code, you may check it here Boss.

Thanks

https://github.com/Predstan/Algorithm-and-Data-Structure/tree/master/ch5/Game%20Of%20Life%20-%20Improved

The first file is the text based implementation of the game, the second is the LifeGrid Class where I used the list and Divide and conquer

1 Like

Re: Common good python programming practices you should know by gbolly1151(m): 9:56pm On Jun 06, 2020
Predstan:


I used the Binary search to insert an element into the right position in the list. So the 0(.) Is just Logn. Instead of returning true or false, it returns the position of an item after divide and conquer, then I use the insert function to insert in the right position in the list.

I also wrote a game called Game of Life where organisms dies or lives with respect to the number of organisms around them that are alive. Its similar code, you may check it here Boss.

Thanks

https://github.com/Predstan/Algorithm-and-Data-Structure/tree/master/ch5/Game%20Of%20Life%20-%20Improved

The first file is the text based implementation of the game, the second is the LifeGrid Class where I used the list and Divide and conquer


Will check it out bro
Re: Common good python programming practices you should know by Should: 10:18pm On Jun 06, 2020
gbolly1151:
What are factory functions?
This is the basic of decorator in python
The below screenshot confused every me, while trying to understand Identity function..
Everything was going smoothly, until those figures appeared below "id(x), id(y)" ..
how they got the value on red...

1 Like

Re: Common good python programming practices you should know by gbolly1151(m): 11:32pm On Jun 06, 2020
Should:

The below screenshot confused every me, while trying to understand Identity function..
Everything was going smoothly, until those figures appeared below "id(x), id(y)" ..
how they got the value on red...
The value in red are the memory addresses where x and y are stored,these values differs with PC. It is your pc that is responsible for how values are stored in the memory, id() only return the memory address
Re: Common good python programming practices you should know by Should: 12:32am On Jun 07, 2020
gbolly1151:

The value in red are the memory addresses where x and y are stored,these values differs with PC. It is your pc that is responsible for how values are stored in the memory, id() only return the memory address
OK, God bless...

1 Like

Re: Common good python programming practices you should know by gbolly1151(m): 8:58am On Jun 07, 2020
Should:
OK, God bless...
Amen
Re: Common good python programming practices you should know by Predstan: 12:03pm On Jun 09, 2020
gbolly1151:

Will check it out bro


I finally used LinkedList for the SparseMatrix and it was just Literarilly better. From Quartic to Quadratic

https://github.com/Predstan/Algorithm-and-Data-Structure/blob/master/ch6/SparseMatrix%20Using%20Sorted%20Linkedlist/sparseMatrix.py

1 Like

Re: Common good python programming practices you should know by gbolly1151(m): 5:57pm On Jun 09, 2020
Predstan:



I finally used LinkedList for the SparseMatrix and it was just Literarilly better. From Quartic to Quadratic

https://github.com/Predstan/Algorithm-and-Data-Structure/blob/master/ch6/SparseMatrix%20Using%20Sorted%20Linkedlist/sparseMatrix.py
But still curious why you dont want to use list

Access time O(1)
Re: Common good python programming practices you should know by Predstan: 6:22pm On Jun 09, 2020
gbolly1151:

But still curious why you dont want to use list

Access time O(1)

Learning different data structures actually. I wrote one that uses List but still quadratic. Because using nested list, I have to iterate over each list of the both Matrices.
One advantage of using the Array or the LinkedList is the memory since List creates double of storage being used.

What I read tho.
Re: Common good python programming practices you should know by gbolly1151(m): 8:49pm On Jun 09, 2020
Predstan:


Learning different data structures actually. I wrote one that uses List but still quadratic. Because using nested list, I have to iterate over each list of the both Matrices.
One advantage of using the Array or the LinkedList is the memory since List creates double of storage being used.

What I read tho.

Hmm....ok
Re: Common good python programming practices you should know by gbolly1151(m): 9:15pm On Jun 09, 2020
Predstan:


Learning different data structures actually. I wrote one that uses List but still quadratic. Because using nested list, I have to iterate over each list of the both Matrices.
One advantage of using the Array or the LinkedList is the memory since List creates double of storage being used.

What I read tho.
Zip two list should reduce it,just thinking
Re: Common good python programming practices you should know by gbolly1151(m): 12:41pm On Jun 11, 2020

'''
what is @property?
@property is a decorator and what is a decorator?

Decorator is a technique used to change the behavior of
an object.

@property is a decorator used to change the behaviour
of a class method,it is use mostly to enable a
class method to behave as an attribute.

Now let illustrate attribute call
'''
class Test:
def __init__(self,var1,var2):
self.var1=var1
self.var2=var2

T1 = Test(2,3)
print('initial var1',T1.var1) #output: initial var1 2
print('initial var2',T1.var2) #output: initial var2 3
#changing the value of var1 and var2
T1.var1=6
T1.var2=10
print('new var1',T1.var1) #output: new var1 6
print('new var2',T1.var2) #output: new var2 10


'''
T1.var1 and T1.var2 are attribute calls

Now let see how method behave when we call it like an attribute
'''
class Test1:
def __init__(self):
self._var3=0

#@property
def var3(self):
return self._var3

#@var3.setter
def var3(self,value):
self._var3=value
T2= Test1()
'''
if you comment @property and then calling T2.var3 will return something like
<bound methoud T1.var3.....>
that is the name given to the function by interpreter
'''
print(T2.var3) #output: <bound methoud T1.var3.....>

'''
with the help of @property we can change how class method will behave
when it is called like an attribute.
in our case, we want the method to return a value and not object name
when it is called like an attribute

now uncomment the @property and @var3.setter
'''

print('initial var3',T2.var3) #output: initial var3 0
#setting new value for _var3
T2.var3 = 4
print('new var3',T2.var3) #output: new var3 4

1 Like 1 Share

Re: Common good python programming practices you should know by gbolly1151(m): 7:40pm On Jun 16, 2020
In need of a special data type(object)?....you create class,

In need of special class?....you create meta class
Re: Common good python programming practices you should know by CuteNerdyDude(m): 9:43pm On Jun 17, 2020
@gbolly1151 please what materials would you recommend for an intermediate python programmer?.
Re: Common good python programming practices you should know by iCode2: 10:40pm On Jun 17, 2020
spam = ['apples', 'bananas', 'tofu', 'cats']

Write a function that takes a list value as an argument and returns a string with all the items separated by a comma and a space, with and inserted before the last item. For example, passing the previous spam list to the function would return 'apples, bananas, tofu, and cats'. But your function should be able to work with any list value passed to it.
Re: Common good python programming practices you should know by gbolly1151(m): 3:11am On Jun 18, 2020
CuteNerdyDude:
@gbolly1151 please what materials would you recommend for an intermediate python programmer?.
realpython.com

1 Like

Re: Common good python programming practices you should know by gbolly1151(m): 3:30am On Jun 18, 2020
iCode2:
spam = ['apples', 'bananas', 'tofu', 'cats']

Write a function that takes a list value as an argument and returns a string with all the items separated by a comma and a space, with and inserted before the last item. For example, passing the previous spam list to the function would return 'apples, bananas, tofu, and cats'. But your function should be able to work with any list value passed to it.
Just seeing this


spam = ['apples', 'bananas', 'tofu', 'cats']

def printlist(List):
last=List.pop()
last=' and '+last
List.append(last)
return ','.join(List)

print(printlist(spam))

1 Like

Re: Common good python programming practices you should know by CuteNerdyDude(m): 8:20am On Jun 18, 2020
Thanks man. �

1 Like

Re: Common good python programming practices you should know by gbolly1151(m): 10:19am On Jun 18, 2020
CuteNerdyDude:
Thanks man. �
you are welcome bro
Re: Common good python programming practices you should know by gbolly1151(m): 10:19am On Jun 18, 2020
If you want a variable or method that should be called;
1) from anywhere outside of a class,then declare the variable/method as public e.g in python name='gbolly'

2) within a class only,then declare the variable/method as private e.g in python __name='gbolly' #double underscore

3) only from class and subclasses then declare it protected
e.g in python _name='gbolly' #single underscore
Re: Common good python programming practices you should know by Grandlord: 11:43am On Jun 18, 2020
gbolly1151:

Just seeing this


spam = ['apples', 'bananas', 'tofu', 'cats']

def printlist(List):
last=List.pop()
last=' and '+last
List.append(last)
return ','.join(List)

print(printlist(spam))
Bad guy cool
Re: Common good python programming practices you should know by gbolly1151(m): 12:00pm On Jun 18, 2020
Grandlord:
Bad guy cool
Lolz...boss.
Re: Common good python programming practices you should know by iCode2: 5:45pm On Jun 18, 2020
gbolly1151:

Just seeing this


spam = ['apples', 'bananas', 'tofu', 'cats']

def printlist(List):
last=List.pop()
last=' and '+last
List.append(last)
return ','.join(List)

print(printlist(spam))
Oh I just checked again now. I misunderstood the question. I was working on returning an item in the list as a string separated by comma with an and before the last item. Something like... Returning the first index as 'a, p, p, l, e, and s'.

Will that work?
Re: Common good python programming practices you should know by emilfischer(m): 7:30pm On Jun 18, 2020
Affordable UK used laptop Brand: Hp Series: Elitebook 2570p Processor: intel corei5 Hard drive: 320gb Ram size: 4gb Screen size: 12 inches Graphics: intel HD graphics Webcam: Available Wireless: Available Dvd rom: Available Fingerprint scanner: Available Supported memory: SDXC, SDHC Ports: Usb ports, vga Operating system: Win10 Charger: Free Software installations: Free
Delivery in Lagos: Pay on delivery
Nationwide pay on delivery

Price: 70k

(1) (2) (3) (4) (5) (6) (7) (8) (Reply)

Can You Survive If You Stopped Working In Tech? / How Do You Overcome Laziness And Dizziness When Working / Dr. Chinedu Emeka Invents Computer Software To Track Criminals

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 49
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.