Common good python programming practices you should know - Programming (4) - Nairaland
Nairaland Forum › Science/Technology › Programming › Common good python programming practices you should know (15169 Views)
1 2 3 4 5 6 7 8 Reply (Go Down)
| Re: Common good python programming practices you should know by Brukx(m): 10:11am On May 11, 2020 |
gbolly1151:Bro, can you recommend any good kivy book? |
| Re: Common good python programming practices you should know by gbolly1151(op): 3:02pm On May 11, 2020*. Modified: 9:12am On May 14, 2020 |
Brukx:I haven't try building mobile app before so don't know the best book out there but you can search for kivy tutorial on Google the first index still look ok |
| Re: Common good python programming practices you should know by gbolly1151(op): 9:13am On May 14, 2020 |
HOW TO USE all() KEYWORD all() in python is a keyword that take iterable object as aguement and return true only when all the elements in this iterable are true '''
|
| Re: Common good python programming practices you should know by gbolly1151(op): 11:07am On May 16, 2020 |
WHAT IS if __name__=='__main__'? #short explanation
then, you are telling the computer that if this python file is run by user or robot then print('hello') #full explanation Anytime you open python file,you are actually loading the file into the memory of the computer (i.e RAM). The name that will be given to that file at that point wont be the name of the file but a new name that is called '__main__' (i.e __name__='__main__') and it is stored together with other data in memory. To check that, open any python file and print global data using global keyword
from that output above we can see that __name__= '__main__' so at runtime the name of any python file is '__main__'. This name is often use proffessionally in python for testing or running the functionality of a script at runtime. you might be wondering why we have underscore,This is because we dont want it to be in conflict when we use 'name' as variable name in our program now,if you use
then, you are telling the computer that if this python file is run by user or robot then print('hello') you can acqually change the variable of __name__ e.g
|
| Re: Common good python programming practices you should know by Predstan: 12:42pm On May 16, 2020 |
gbolly1151:I finally learnt OOP. I have completed a project. Its coordinate of a line with slope, distance and to determine if a line is vertical or horizontal and if it is perpendicular or parallel to another. I also completed a Time project to display the julian day, the day of the week, of that date and gregorian date. Its interesting so far. I couldn't display the calendar without the normal calendar module. I am trying to create my own calendar module but I didnt get that. I'm also currently working on Polygon. |
| Re: Common good python programming practices you should know by gbolly1151(op): 1:41pm On May 16, 2020 |
Predstan:Nice one bro...keep it up |
| Re: Common good python programming practices you should know by iCode2: 11:55am On May 18, 2020 |
Hey guys, so, I'm trying my hands on this exercise: The Collatz Sequence Write a function named collatz() that has one parameter named number. If number is even, then collatz() should print number // 2 and return this value. If number is odd, then collatz() should print and return 3 * number + 1. Then write a program that lets the user type in an integer and that keeps calling collatz() on that number until the function returns the value 1. (Amazingly enough, this sequence actually works for any integer—sooner or later, using this sequence, you’ll arrive at 1! Even mathematicians aren’t sure why. Your program is exploring what’s called the Collatz sequence, sometimes called “the simplest impossible math problem.”) Remember to convert the return value from input() to an integer with the int() function; otherwise, it will be a string value. Hint: An integer number is even if number % 2 == 0, and it’s odd if number % 2 == 1. Here's my code:
What am I not doing right? |
| Re: Common good python programming practices you should know by Predstan: 12:33pm On May 18, 2020*. Modified: 1:06pm On May 18, 2020 |
iCode2:First you need to return the number from the Collatz function as an integer not a print function... use: return number//2. Then you have to check that the return value from the Collatz is not equal to 1 if not, user keeps inputting number and the Collatz function is called on the number from user.
|
| Re: Common good python programming practices you should know by gbolly1151(op): 4:44pm On May 18, 2020 |
Predstan:That code for recursion doesn't look like one and even the first method will still end up looping and wont end. Am thinking should look like this
|
| Re: Common good python programming practices you should know by gbolly1151(op): 5:04pm On May 18, 2020 |
Or there is something i don't get? |
| Re: Common good python programming practices you should know by Predstan: 5:40pm On May 18, 2020 |
gbolly1151:Ok, But on the other hand, look at this bolded part of the question. I was thinking, we should be checking that return value is not equal to 1 and not our input value iCode2: |
| Re: Common good python programming practices you should know by gbolly1151(op): 7:05pm On May 18, 2020 |
Predstan:Have you tried to run the code and it works as you expected? |
| Re: Common good python programming practices you should know by iCode2: 7:44pm On May 18, 2020 |
Predstan:Thanks but it didn't give the right output. |
| Re: Common good python programming practices you should know by iCode2: 7:45pm On May 18, 2020 |
gbolly1151:I got the correct output from this. Thanks |
| Re: Common good python programming practices you should know by Predstan: 8:05pm On May 18, 2020 |
iCode2:Oh Sorry, I hope I haven't confused you |
| Re: Common good python programming practices you should know by iCode2: 8:16pm On May 18, 2020 |
Predstan:Nope.. |
| Re: Common good python programming practices you should know by iCode2: 8:17pm On May 18, 2020 |
Can anyone recommend good python books with a lot of exercises? I just started out with automate the boring stuff. |
| Re: Common good python programming practices you should know by gbolly1151(op): 8:30pm On May 18, 2020 |
iCode2:Download think python |
| Re: Common good python programming practices you should know by iCode2: 10:48pm On May 18, 2020 |
gbolly1151:Alright, thanks. |
| Re: Common good python programming practices you should know by DanRay1: 8:55pm On May 19, 2020 |
gbolly1151:How does this code repeatedly prompt the user for input when the input line isn't in the while loop block? Or is it in the collatz function? Because it doesn't look like you put it there. If it's in the function, then indent it a bit to make it readable and clearer. |
| Re: Common good python programming practices you should know by gbolly1151(op): 9:13pm On May 19, 2020 |
DanRay1:We are only required to input a number then it will print out all the collatz number still 1 |
| Re: Common good python programming practices you should know by gbolly1151(op): 9:27pm On May 19, 2020 |
DanRay1:
Hope it is readable? |
| Re: Common good python programming practices you should know by DanRay1: 1:40am On May 20, 2020 |
gbolly1151:Okay I get it now. |
| Re: Common good python programming practices you should know by gbolly1151(op): 4:33pm On May 20, 2020 |
How to create a simple singleton without decorator Decorator is one of the popular way of creating singleton in python but we wont be using decorator here. before i move to code let me define singleton,it is a way of creating a single object of a class or function. In a program where you only need a single instance of a class you can create a Singleton.
|
| Re: Common good python programming practices you should know by darepapi: 4:15pm On May 21, 2020 |
gbolly1151:I'd prefer the dunder method "repr"....(__repr__) without explicitly calling the print function on the object, repr returns the object description.... I.e instead of print(obj) running "obj" alone works better |
| Re: Common good python programming practices you should know by gbolly1151(op): 2:32pm On May 22, 2020 |
REMINDER: Range has step argument range(start,stop,step) Note that start and step are optional How to use step in range to print odd and even
|
| Re: Common good python programming practices you should know by gbolly1151(op): 3:43pm On May 22, 2020 |
HOW TO SWAP AN OBJECT
|
| Re: Common good python programming practices you should know by iCode2: 7:14pm On May 22, 2020 |
Hi gbolly1151, you seem to be very good with Python. When did you start learning? And are you self-taught? |
| Re: Common good python programming practices you should know by gbolly1151(op): 9:39pm On May 22, 2020 |
iCode2:I started learning python few years ago and am a self-taught |
| Re: Common good python programming practices you should know by gbolly1151(op): 7:02pm On May 24, 2020 |
HOW TO USE LAMBDA The format of lambda is as below [lambda keyword] [arguments[seperated by comma]] [semicolon] [logic]
|
| Re: Common good python programming practices you should know by gbolly1151(op): 2:57pm On May 27, 2020 |
KEY DIFFERENCE BETWEEN is AND == is operator is use to check if two object have the same id number == is use to check if two objects have the same value
|
| Re: Common good python programming practices you should know by gbolly1151(op): 3:49pm On May 27, 2020 |
PLEASE AVIOD THIS MISTAKE IN YOUR CODE
|
I Need A Good Python Programmer (offer has been taken) • I'm Looking For A Very Good Python Tutor. • Why Is Python Programming So Hard To Understand • 2 • 3 • 4
Is Java Programming Harder Than Microsoft.net Programming • ATM Machine Programming Language. • Learn Android App Development For FREE Here