NewStats: 3,263,913 , 8,181,864 topics. Date: Sunday, 08 June 2025 at 06:22 PM 2x1f6f

6z3e3g

Common good python programming practices you should know - Programming - Nairaland 3x3a4e

Common good python programming practices you should know (14474 Views)

(4)

(1) Go Down)

gbolly1151(m): 9:04am On Nov 26, 2019
Hello this thread is open to post series of code in python that can less the amount of code written for simplicity in coding and creating your project with less code as possible.

I wish this thread will be clean as possible hope the can grant the creator of each post power to screen out spammers
gbolly1151(m): 9:14am On Nov 26, 2019
Am starting with list

You can populate your list with a single line of code



#old coding practice to populate list object
Elements = []
for element in range (10):
Elements.append(element)
print(Elements)

#new coding practice to populate list object
Elements = [ element for element in range(10) ]

3 Likes

gbolly1151(m): 9:57am On Nov 26, 2019

#how to print negative numbers
Elements = [ element for element in range (0,-10,-1)]
gbolly1151(m): 10:31am On Nov 26, 2019

#old way of finding square of a number
def old_square (num):
return num*num

#fastest way
square = lambda num : num*num

print(old_square (5)) #output is 25
Print(square (5)) #output is 25

1 Like

gbolly1151(m): 11:46pm On Nov 27, 2019
chidemavian:
There are factually no cheats in Python nor any other language.


What u tried to demonstrate in the first example is known as list comprehension.



Its good practice in programming, as is in other professions, to give the right name to the right concept




Don't misslead the public


Thanks.


If u want to reach me.....click here

https:///23407031009036

I personally called them cheat because they are short cut in getting things done....dont be offended if the naming sounds bad to you
Nmeri17: 12:29am On Nov 28, 2019
Python sweet sha. Plenty syntactic sugar. If you're unlucky and get hooked, you may find it difficult to detach. Rough guess sha cuz I've only used it to setup an RPC server between my dominant language.

It strikes me as very feminine — object and method name concision, package names, ease of use etc.

Nice thread op

4 Likes

gbolly1151(m): 7:43am On Nov 28, 2019

#old way of file operation
f=open('test.txt','rb')
f.write(b'hello world')
f.close()

#fast way
with open('test.txt','rb') as f:
f.write(b'hello world')
Ajibel(m): 3:54pm On Nov 28, 2019
Check out this link

https://books.agiliq.com/projects/tweetable-python/en/latest/

You may enjoy what you see smiley

2 Likes

crunchyDope(m): 7:09pm On Nov 28, 2019
Nmeri17:
Python sweet sha. Plenty syntactic sugar. If you're unlucky and get hooked, you may find it difficult to detach. Rough guess sha cuz I've only used it to setup an RPC server between my dominant language.

It strikes me as very feminine — object and method name concision, package names, ease of use etc.

Nice thread op
Lol feminine language? this one loud and sounds true!

too easy!

2 Likes

gbolly1151(m): 12:24am On Nov 29, 2019


#in some cases you may want to assign value to some variable that are nearing #in the same category or having mutual relationship

#old way is

a="hello"
b="world"
c="!!!"

#good programming is
a,b,c='hello', 'world', '!!!'

1 Like

gbolly1151(m): 12:29am On Nov 29, 2019

a,b,c= 'hey','hello','world'

#old string formating
print(a,b,c)

#new string formatting
print(f'{a} {b} {c}')
gbolly1151(m): 11:28am On Nov 30, 2019

#bad style
X = X + 1

#good style
X += 1 #can be use for other arithmetic operations
gbolly1151(m): 11:36am On Nov 30, 2019

# To accept multiple variable into a function
def num(*arg):
print(*arg)

num(1,2,3,4) # output = 1 2 3 4
num(1) #output=1
num('hello','world') #output=hello world

# To accept multiple keyword variable into a function
def num(**a):
for i,k in a.items():
print(k)


num(a="heloo",b="5" ) #output hello,5



gbolly1151(m): 12:02pm On Nov 30, 2019

#how to output dict variable

Nums = {"one" : 1,"two" =2, "three" : 3 }
for key,value in nums.items():
print(f'{key} - {value}')

#output
# one - 1
#two - 2
#three - 3
gbolly1151(m): 12:45am On Dec 03, 2019

#how to arrange a list in ascending order
sorted([3,6,1,2]) #output [1,2,3,6]

#in decending order
sorted([3,6,1,2],reversed = True) # [6,3,2,1]
gbolly1151(m): 7:50am On Dec 06, 2019

# how to search for index of multiple chars in a string
s='sleep'

#first index of e
# hint: the syntax of index method is index( string, start, stop)
# string - the string to search
# start - where to Begin search,by default search Begin at 0 i.e the beginning of the string
# stop - where search to stop ,by default search stop at the end of the string

f_index = s.index('e') #output = 2

S_index =s.index('e', 3) #output = 3

#using iteration to find more than 2 chars in a string

s=' sleeping eeee '
Start_position=0
for i in range(s.count('e')):
Start_position = s.index('e' , Start_position)
print(f' e found at position: {Start_position}')
Start_position+=1

Re: Common good python programming practices you should know by Nobody: 12:52am On Dec 08, 2019
gbolly1151:
Hello this thread is open to post series of code in python that can less the amount of code written for simplicity in coding and creating your project with less code as possible.

I wish this thread will be clean as possible hope the can grant the creator of each post power to screen out spammers
pls which tool can i use too execute a program like python

1 Like

gbolly1151(m): 12:47pm On Dec 08, 2019
Jesse25:

pls which tool can i use too execute a program like python
I don't really your question.... Do you mean IDE?
melodyogonna(m): 1:13pm On Dec 08, 2019
Jesse25:
pls which tool can i use too execute a program like python
the Python interpreter for your OS from Python. org

1 Like

melodyogonna(m): 1:14pm On Dec 08, 2019
Nice thread OP

1 Like

gbolly1151(m): 6:17pm On Dec 08, 2019

#Run math python script and expression with eval()
#Take for instance input = 2+4*5-6
#instead of writing long algorithm you can solve easily with eval()

Math_expression = input(' type your math expression :')
try:
Ans = eval(math_expression)
print(f'output : {Ans}')
except:
print('invalid basic math operations')

Input : 2+4*5-6
Output : 24
gbolly1151(m): 6:20pm On Dec 08, 2019
gbolly1151:

#Run math python script and expression with eval()
#Take for instance input = 2+4*5-6
#instead of writing long algorithm you can solve easily with eval()

Math_expression = input(' type your math expression :')
try:
Ans = eval(math_expression)
print(f'output : {Ans}')
except:
print('invalid basic math operations')

Input : 2+4*5-6
Output : 24

Be careful when using this function because your system can be access through this....the eval() just help to run python script which means any python script can be run through eval()
Re: Common good python programming practices you should know by Nobody: 11:56pm On Dec 08, 2019
gbolly1151:

I don't really your question.... Do you mean IDE?
I mean the website I can use you run d program
Re: Common good python programming practices you should know by Nobody: 11:58pm On Dec 08, 2019
gbolly1151:

I don't really your question.... Do you mean IDE?
I mean the website I can use too run d program
gbolly1151(m): 8:46am On Dec 09, 2019
Jesse25:

I mean the website I can use too run d program

Website for Hosting your code?
gbolly1151(m): 9:19am On Dec 16, 2019


#how to map two lists of equal length together
y=[7,8,3]
x=[1,2,3]
for i in zip(y,x):
print(i)

Output
(7,1)
(8,2)
(3,3)

Samsonari: 11:37am On Dec 16, 2019
gbolly1151:


#how to map two lists of equal length together
y=[7,8,3]
x=[1,2,3]
for i in zip(y,x):
print(i)

Output
(7,1)
(8,2)
(3,3)



Thanks ���

1 Like

gbolly1151(m): 11:26am On Dec 17, 2019

num=list(range(5))
print(num)

#output [1,2,3,4]
gbolly1151(m): 4:32pm On Dec 23, 2019


#set is used for removing duplicate from ordered list

Num=set(1,1,3,2,6,2,4,5)

#output = (1,2,3,4,5,6)
gbolly1151(m): 8:25am On Dec 31, 2019
#to get help info about any particular module or inbuilt function you can use help() let see how to use below

print( help(int))

output:

(1) Reply)

Alt School Africa Thread

(Go Up)

Sections: How To . 28
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or s on Nairaland.