Overclock.net - An Overclocking Community (https://www.overclock.net/forum/)
-   Coding and Programming (https://www.overclock.net/forum/142-coding-programming/)
-   -   What is this? Not a string.. (https://www.overclock.net/forum/142-coding-programming/1711038-what-not-string.html)

 PhotonFanatic 10-17-2018 03:53 PM

What is this? Not a string..

What is this? Its not a string. This is part of my python lessons and I'm trying to figure out what exactly it is, and if this can actually be equal. Using python 2 not 3 if that matters. Paying special attention to the part that starts at Cleese. How can Cleese equal King Arthur? And what does the ' mean?

Code:

`2 ** 3 == 108 % 100 or 'Cleese' == 'King Arthur'`

My understanding is that a string is " and not '

 deepor 10-17-2018 04:45 PM

Just try it with Python's interactive shell. Here's what happens for me:

Code:

```Python 3.7.0 (default, Sep 15 2018, 19:13:07) [GCC 8.2.1 20180831] on linux Type "help", "copyright", "credits" or "license" for more information. >>> print("hello") hello >>> print('hello') hello >>> "hello" == 'hello' True >>>```
It seems single quotes work fine and can be used for literal text just like double quotes.

 PhotonFanatic 10-17-2018 05:12 PM

So this IS a string then?

Code:

`'Here is my string. It is a rather nice string.'`

Next question: Can someone write this out mathematically instead of in python speak?

100 ** 0.5

100 to the power of 0.5. That just makes no sense to me. Never considered it, wasn't taught that in school. To me, they gotta be whole numbers. You have to say something like 2 x 2 x 2 x 2 x 2. And then of course you'd have 2 ** 5 in python. That looks much better to me. What would 100 ** 0.5 be?

 Dagamus NM 10-17-2018 05:15 PM

Quote:
 Originally Posted by PhotonFanatic (Post 27672734) So this IS a string then? Code: `'Here is my string. It is a rather nice string.'` Next question: Can someone write this out mathematically instead of in python speak? 100 ** 0.5 100 to the power of 0.5. That just makes no sense to me. Never considered it, wasn't taught that in school. To me, they gotta be whole numbers. You have to say something like 2 x 2 x 2 x 2 x 2. And then of course you'd have 2 ** 5 in python. That looks much better to me. What would 100 ** 0.5 be?
Wouldn't 100 ** 0.5 be 10? SQRT(100)?

 deepor 10-17-2018 06:10 PM

Yeah, 100 ** 0.5 is the same as sqrt(100). Here's how you can explain how that happens:

Let's check out a super simple example to think about what exponentiation is:

10 ** 3
= 1000

You know how that example works, this here is happening behind the scenes:

10 ** 3
= 10 * 10 * 10
= 1000

Playing around with things a bit, you can do this here:

10 ** 3
= 10 ** (2 + 1)
= (10 ** 2) * (10 ** 1)
= 100 * 10
= 1000

Now going back to that 100 ** 0.5 which is confusing, let's think about 100 ** 1 first:

100 ** 1
= 100

Let's split that "1" into two halves:

100 ** 1
= 100 ** (0.5 + 0.5)
= (100 ** 0.5) * (100 ** 0.5)

Do you see what's happening? That's where the answer for 100 ** 0.5 is coming from, and it's "10":

100 ** 0.5
= 10

 PhotonFanatic 10-18-2018 04:44 PM

Thanks for the explanation it is much appreciated! Yes I can see what is happening, but I think that I don't like it lol. It seems to me that there are far better and more efficient and even more sensible ways of saying it. Probably why they just don't in normal math speak. The sqrt is perfect imo.

Anyway I'm glad you explained it. Now I'll move on to my next question. In the code below, there is an error in the logic, at least to me.

Code:

```# Complete the if and elif statements! def grade_converter(grade):     if grade >= 90:         return "A"     elif grade >= 80:         return "B"     elif grade >= 70:         return "C"     elif grade >= 65:         return "D"     else:         return "F"       # This should print an "A"      print grade_converter(92) # This should print a "C" print grade_converter(70) # This should print an "F" print grade_converter(61)```
Near the top, how can you say if the grade is = or > 90, then return an A. But then the next thing you say, is that if the grade is > or = an 80, then you return a B. Because 90-100 are also greater than 80. So wouldn't this confuse python and it wouldn't know whether to give you an A, or a B? If in fact you earned a score that was somewhere in the 80-89 range.

Quote:
 Originally Posted by PhotonFanatic (Post 27674506) Thanks for the explanation it is much appreciated! Yes I can see what is happening, but I think that I don't like it lol. It seems to me that there are far better and more efficient and even more sensible ways of saying it. Probably why they just don't in normal math speak. The sqrt is perfect imo. Anyway I'm glad you explained it. Now I'll move on to my next question. In the code below, there is an error in the logic, at least to me. Code: ```# Complete the if and elif statements! def grade_converter(grade):     if grade >= 90:         return "A"     elif grade >= 80:         return "B"     elif grade >= 70:         return "C"     elif grade >= 65:         return "D"     else:         return "F"       # This should print an "A"      print grade_converter(92) # This should print a "C" print grade_converter(70) # This should print an "F" print grade_converter(61)``` Near the top, how can you say if the grade is = or > 90, then return an A. But then the next thing you say, is that if the grade is > or = an 80, then you return a B. Because 90-100 are also greater than 80. So wouldn't this confuse python and it wouldn't know whether to give you an A, or a B? If in fact you earned a score that was somewhere in the 80-89 range.
In normal math speak, it is perfectly acceptable to raise something to the 1/2 power. In fact it is very common.

Your code will only get to the > or = 80 portion if the grade is less than 90. It's executing those else-if (elif) statements in order from the top down. Same thing for C, it'll only get to the 3rd statement if the first two haven't been met. Hopefully that makes sense.

 Dagamus NM 10-18-2018 05:48 PM

I am trying to decide if I want to take a python class next semester or Adobe Illustrator. I work full time and will be taking an access class that I need for work. I need to take a second class to keep my student loans away.

Sorry to threadjack, was reading and just started typing what I was thinking.

 PhotonFanatic 10-20-2018 11:03 PM

Quote:
 Originally Posted by xToaDx (Post 27674520) Your code will only get to the > or = 80 portion if the grade is less than 90. It's executing those else-if (elif) statements in order from the top down. Same thing for C, it'll only get to the 3rd statement if the first two haven't been met. Hopefully that makes sense.
I was thinking that maybe since 90+ was already assigned something else, maybe it would just exclude it. But yeah top down makes sense.

Quote:
 Originally Posted by Dagamus NM (Post 27674588) I am trying to decide if I want to take a python class next semester or Adobe Illustrator. I work full time and will be taking an access class that I need for work. I need to take a second class to keep my student loans away.
I'm doing code academy. I'd definitely recommend it as it is interactive. And its free for main portion. If you want some quizzes, and a several projects, then you can pay \$20 per month for a few months in order to gain access to that. But there are only so many extras, so you could pay for a few months and then just turn it off when you're done. I am doing code academy in order to get ready for the python class which will start in early january iirc. I want to make sure there is no way I will fall behind in the class. I don't want to have to take it a second time.

I'm hoping that one class will be all it takes, to make me ready to seek work programming in python language. They say you can learn to code in a few months, so we'll see if that is true. Gotta find out if there is more than one class for python.

 deepor 10-21-2018 02:16 AM

That "elif" has the same meaning as doing this nested structure of "if"s here:

Code:

```if grade >= 90:     return "A" else:     if grade >= 80:         return "B"     else:         if grade >= 70:             return "C"         else:             if grade >= 65:                 return "D"             else:                 return "F"```
That "elif" is there so that you can write this in a shorter way without this ridiculous nesting, but it's still the same meaning.

Quote:
 Originally Posted by PhotonFanatic (Post 27674506) [...] Near the top, how can you say if the grade is = or > 90, then return an A. But then the next thing you say, is that if the grade is > or = an 80, then you return a B. Because 90-100 are also greater than 80. So wouldn't this confuse python and it wouldn't know whether to give you an A, or a B? If in fact you earned a score that was somewhere in the 80-89 range.
Python is not getting confused because it's not doing all of those "if + elif + elif + ..." questions at the same time. It's working through that nested structure of "else" paths and working on just one "if" question at a time.

All times are GMT -7. The time now is 12:55 PM.