Python Help For Questions - python

I would like some help with a question on Python. Does anyone know why the coding language is so complicated to learn for some people? I need an answer in 1 week from now.
Thanks.

In my opinion it isnt really that hard. Although, I do forget things here and there. I’d suggest using 3.6 if you are a beginner. Its where I started. If you need a sample program(s), you may use the following.
Printing to console:
print(‘hello’)
Printing to console using variables:
thisIsAString = ‘hello’
print(thisIsAString)
Here are some conditions.
TrueBoolean = True
TrueString = ‘Yes’
TrueInt = 1
TrueList = [‘Yes’, 1, True]
if TrueBoolean:
print(‘Boolean is True’)
if TrueString == ‘Yes’:
print(‘String is “Yes”’)
if TrueInt == 1:
print(‘Int is “Yes”’)
if TrueList == [‘Yes’, 1, True]: # TrueList.contains(INSERT ITEMS HERE) works too.
print(‘List is true’)

Related

Is there a difference between these codes?

I am a beginner in coding and I've been learning python just lately.
I tried to do some recursion exercises, my codes run most of the time but there is one thing that is worrying me.
My code is always longer than the one in the solution.
Take this exercise for example
(Write a Python program to get the sum of a non-negative integer.Test Data:sumDigits(345) -> 12sumDigits(45) -> 9)
My answer is this
def sum_num(n,sum1=0):
k=int(n)
if k%10==k:
return sum1+k
else:
sum1+=k%10
k=k//10
return(sum_num(k,sum1))
print(sum_num(2384))
and the solution is this
def sumDigits(n):
if n == 0:
return 0
else:
return n % 10 + sumDigits(int(n / 10))
print(sumDigits(345))
print(sumDigits(45))
print(sumDigits(345))
print(sumDigits(45))
My code run perfectly but it's just longer. My question is 'is it okay to have a long code, if not how can I learn to shorten it'.
Thank you
Well, writing shorter and cleaner code is good practice because when you have long code thats hard to read its going to be harder for you and especially oher people to work with it, and troubleshooting can be really difficult. Think simple and try to write it as clean as it could possibly be. There isnt really a way to LEARN it but practice and studying and just trying to think simple should help.

using python nonzero function inside array brackets

I'm looking at the linked GitHub repository and working through the MLA machine learning book, and stumbled across this bit of code and don't understand what's going on here. What is happening here? Is there a better, more explicit way of writing this bit of python?
https://github.com/KUpypy/Past/blob/fd981bfcc3599914be71d38f0412ef2edbcf6046/MLA/(MLA)%20Chp10/(MLA)%20Chp10_Grouping%20unlabeled%20items%20using%20k-means%20clustering.ipynb
bestClustAss[nonzero(bestClustAss[:,0].A == 1)[0], 0] = len(centList)
bestClustAss[nonzero(bestClustAss[:,0].A == 0)[0], 0] = bestCentToSplit
This function is throwing the following error:
local variable 'bestClustAss' referenced before assignment
My thought was something like this:
if(nonzero(bestClustAss[:,0].A == 1)
bestClustAss ....... = len(cenList)
if(nonzero(bestClustAss[:,0].A == 0)
bestClustAss ....... = bestCentToSplit
But I'm still too new to python to figure this out on my own.

Indentation Error in the Usage of 'def' in Python 3.7

def day_display_control(entered_variable,controlling_number,return_message):
if entered_variable == controlling_number:
return(return_message)
I have been trying to find a problem here, as IDLE keeps on giving an indentation error saying expected an indented block yet I have found none so far, my indentation width is 4 and I tried using only tab as well, haven't found the solution, thank you in advance as this is probably a really basic question.
P.S:
I have also tried to debug the rest of the code without this line, yet while this gives the same error:
def day_display():
day_display_number = day % 7
day_display = day_diplay_control(day_display_number,0,Monday)
day_display = day_diplay_control(day_display_number,1,Thuesday)
day_display = day_diplay_control(day_display_number,2,Wednesday)
day_display = day_diplay_control(day_display_number,3,Thursday)
day_display = day_diplay_control(day_display_number,4,Friday)
day_display = day_diplay_control(day_display_number,5,Saturday)
day_display = day_diplay_control(day_display_number,6,Sunday)
Do not mind the quality of the code, the problem is that previous 'def's don't cause this to happen, such as;
def typeWait(message,delay):
message = str(message)
print(message)
sleep(delay)
P.P.S: I have just realized, as of yesterday, that python was not 3.6 anymore, but instead 3.7 alpha 2, which leads me to believe that this is either a new feature or a bug, I have found no articles about either of them yet, so if anyone knows what the problem is, I would appriciate it a lot.
I agree with #jasonharper. Check if you mistyped = instead of ==, and also if any of these are missing: ), ] or }. It might not be near the block of these functions.

How would I make what the user typed for else into a variable/input?

else=(x = raw_input("Enter\n")):
print(x)
I have tried doing this basing it off of the correct statement...
x = raw_input("Enter\n")
print(x)
if your doing an if statement and you wanted the input after else is ran you would just run it as normal in the if/else flow.
But depending on what you are actually trying to do its likely to have a better way to do it. Not sure your skill level but i would HIGHLY recommend Jessica McKellers basic tutorials on youtube or the basic Python tutorial like Tigerhawk recommended.
if something:
print("Hello")
else:
x = raw_input("Enter Something ")
print(x)

Python - What's the use of if True:?

I just came accross the following code in an existent project, which I'm working on:
if True:
x = 5
y = 6
return x+y
else:
return 'Something
Inside the if True are lots of conditions and some will also return the function already.
Why would somebody write in that way? The code contained some other bugs also, but was just wondering about the if True: statement as it didn't make any sense to me. Probably also pretty stupid to ask it, but was wondering hehe.
It might be a remnant of debugging or refactoring. It may be that instead of True, there was orginally a condition or variable there but it has now been replaced by True. The developer perhaps left it there without refactoring or cleaning it up all the way.
If you're free to edit the code as you wish and you're sure that the else is no longer needed, then you can remove it. It indeed makes no sense to have code in your codebase that will never run.
True doesn't necessarily mean True
True = False
if not True :
print "True is false" # This prints ok
Honestly, I don't think anyone would code like this.
Does not make any sense to me, my guess is that someone wanted to have two distinct code paths that he could alternate between a'la using #if 1 .. #else -> #if 0 ... for debugging or such purposes.
Other possibility was that, as #SimeonVisser suggested, the original developer was refactoring or cleaning up the code (and did not have an emulator that allows one to easily remove 1 step of indentation from a block of code)
It could be a flag used for debugging.
It's simply used to ensure that the else: block is never executed.
I have used if True: for some blocks to ensure that my code really does what I want. Usage for debugging or refactoring.
All in all it makes no real sense to use this in an application but for testing or debugging it's somehow acceptable.

Categories

Resources