Welcome, Guest: Join Nairaland / LOGIN! / Trending / Recent / New
Stats: 2,444,048 members, 5,502,905 topics. Date: Wednesday, 01 April 2020 at 03:32 AM

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

Nairaland Forum / Science/Technology / Programming / Common good python programming practices you should know (2426 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) (Reply) (Go Down)

Common good python programming practices you should know by 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 admin can grant the creator of each post power to screen out spammers
Re: Common good python programming practices you should know by 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) ]

1 Like

Re: Common good python programming practices you should know by gbolly1151(m): 9:57am On Nov 26, 2019

#how to print negative numbers
Elements = [ element for element in range (0,-10,-1)]
Re: Common good python programming practices you should know by 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

Re: Common good python programming practices you should know by 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://wa.me/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
Re: Common good python programming practices you should know by 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

2 Likes

Re: Common good python programming practices you should know by 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')
Re: Common good python programming practices you should know by 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

1 Like

Re: Common good python programming practices you should know by 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

Re: Common good python programming practices you should know by 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', '!!!'

Re: Common good python programming practices you should know by 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}')
Re: Common good python programming practices you should know by gbolly1151(m): 11:28am On Nov 30, 2019

#bad style
X = X + 1

#good style
X += 1 #can be use for other arithmetic operations
Re: Common good python programming practices you should know by 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



Re: Common good python programming practices you should know by 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
Re: Common good python programming practices you should know by 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]
Re: Common good python programming practices you should know by 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 Jesse25(m): 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 admin 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

Re: Common good python programming practices you should know by 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?
Re: Common good python programming practices you should know by melodyogonna(m): 1:13pm On Dec 08, 2019
Jesse25:
pls which tool can i use too execute a program like python
download the Python interpreter for your OS from Python. org

1 Like

Re: Common good python programming practices you should know by melodyogonna(m): 1:14pm On Dec 08, 2019
Nice thread OP

1 Like

Re: Common good python programming practices you should know by 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
Re: Common good python programming practices you should know by 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 Jesse25(m): 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 Jesse25(m): 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
Re: Common good python programming practices you should know by lecxis: 4:57am On Dec 09, 2019
How savvy are you on the latest dev tools and technologies? All developers who complete the survey and register enter a raffle to win $16,000 worth of prizes. Get your community involved too - promote the survey and you may win in addition $1,000 in cash!


https://developereconomics.net/?member_id=pdSrFATx&lang=en

Strictly for developers or those learning any programming language

Make your voice heard and earn cool cash and swags as a developer.
Re: Common good python programming practices you should know by 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?
Re: Common good python programming practices you should know by 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)

Re: Common good python programming practices you should know by 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

Re: Common good python programming practices you should know by gbolly1151(m): 11:26am On Dec 17, 2019

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

#output [1,2,3,4]
Re: Common good python programming practices you should know by 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)
Re: Common good python programming practices you should know by 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) (2) (Reply)

Get Free Wordpress Website + Free Domain Name + Free Hosting For A Year / C++ Vs. Java / Enterprise E-learning Portal Development

(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 - 2020 Oluwaseun Osewa. All rights reserved. See How To Advertise. 78
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.