What is this? Not a string.. - Page 2 - Overclock.net - An Overclocking Community

Forum Jump: 

What is this? Not a string..

Reply
 
Thread Tools
post #11 of 44 (permalink) Old 10-22-2018, 04:41 PM - Thread Starter
New to Overclock.net
 
PhotonFanatic's Avatar
 
Join Date: Dec 2009
Posts: 1,832
Rep: 18 (Unique: 17)
Quote: Originally Posted by deepor View Post
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.



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.
Couldn't you never use elif? If that is what you really wanted to do, for whatever reason. Couldn't you just have an endless string of elses? Or would there come a time when that wouldn't really work out, and you need the elif?

"Executing execution.exe"
Bang for the buck
(12 items)
CPU
3570k @ 4.6Ghz @1.35v
Motherboard
Gigabyte Z77x UD3H
GPU
evga GTX 970 SSC
RAM
16Gb Gskill Ripjaws 2133Mhz @ 9-11-10-28
Hard Drive
Samsung Evo 850
Power Supply
Silverstone 900 watt
Cooling
Thermalright Ultra 120 Extreme
Case
Silverstone Raven (1st edition)
Operating System
Windows 10 Spy Delux
Monitor
Xstar 27" PLS 1440p
Keyboard
Wireless ergonomic
Mouse
Logisys Wireless trackball
▲ hide details ▲
PhotonFanatic is offline  
Sponsored Links
Advertisement
 
post #12 of 44 (permalink) Old 10-22-2018, 04:57 PM - Thread Starter
New to Overclock.net
 
PhotonFanatic's Avatar
 
Join Date: Dec 2009
Posts: 1,832
Rep: 18 (Unique: 17)
Also remembered another question I had about your code.

I notice that it all slants to the right of the screen. Is there any particular reason for that? On the codeacademy site, there are indentations, but usually it sort of begins back at the left hand side of the screen. Just wanted to know if this is how the site does things, or if its normal. To me it looks like normally code is written quite slanted.

"Executing execution.exe"
Bang for the buck
(12 items)
CPU
3570k @ 4.6Ghz @1.35v
Motherboard
Gigabyte Z77x UD3H
GPU
evga GTX 970 SSC
RAM
16Gb Gskill Ripjaws 2133Mhz @ 9-11-10-28
Hard Drive
Samsung Evo 850
Power Supply
Silverstone 900 watt
Cooling
Thermalright Ultra 120 Extreme
Case
Silverstone Raven (1st edition)
Operating System
Windows 10 Spy Delux
Monitor
Xstar 27" PLS 1440p
Keyboard
Wireless ergonomic
Mouse
Logisys Wireless trackball
▲ hide details ▲
PhotonFanatic is offline  
post #13 of 44 (permalink) Old 10-22-2018, 05:46 PM
New to Overclock.net
 
deepor's Avatar
 
Join Date: Feb 2013
Posts: 4,713
Rep: 472 (Unique: 318)
Quote: Originally Posted by PhotonFanatic View Post
Also remembered another question I had about your code.

I notice that it all slants to the right of the screen. Is there any particular reason for that? On the codeacademy site, there are indentations, but usually it sort of begins back at the left hand side of the screen. Just wanted to know if this is how the site does things, or if its normal. To me it looks like normally code is written quite slanted.
Yeah, for that example it's important that it looks exactly like that and travels towards the right with each new "else".

In Python you are required to use indentation to do the structure of your code. In other languages, there's instead brackets or words used, and the indentation is just there to make things easier to look at. For example, another language might do it like this:

Code:
if (grade >= 90) {
    return "A"
} else {
    ...
}
Or it might do this:

Code:
if grade >= 90 then
    return "A"
else
    ...
fi
In those languages, the indentation is only done to make the structure easier to see for people. You could remove all indentation and also the line-breaks and the program would still work the same as far as the computer is concerned. Python works differently and the indentation has a meaning.

This whole indentation thing is important for examples like this here:

Code:
if aaa:
    bbb
    ccc
ddd

if aaa:
    bbb
ccc
ddd
The indentation tells Python if the "ccc" line is inside the "if" or outside.
deepor is offline  
Sponsored Links
Advertisement
 
post #14 of 44 (permalink) Old 10-23-2018, 07:25 PM - Thread Starter
New to Overclock.net
 
PhotonFanatic's Avatar
 
Join Date: Dec 2009
Posts: 1,832
Rep: 18 (Unique: 17)
Very nice. If i can continue to pick your brain, I'd like to ask a question about all the various ways that % can be used. I've noticed that there are indeed many of them. I have seen %s, %f, %d, %02, %04 ( iirc ) and others that I can't even remember right now. Is there a list somewhere, that will list off all the various types of % that I could use as a reference? Until I get them memorized. For example I saw this today in a code academy lesson and it seemed like I couldn't really read the code because I haven't seen %d before.


Code:
def square(n):
  squared = n ** 2
  print "%d squared is %d." % (n, squared)
  return squared

square(10)

"Executing execution.exe"
Bang for the buck
(12 items)
CPU
3570k @ 4.6Ghz @1.35v
Motherboard
Gigabyte Z77x UD3H
GPU
evga GTX 970 SSC
RAM
16Gb Gskill Ripjaws 2133Mhz @ 9-11-10-28
Hard Drive
Samsung Evo 850
Power Supply
Silverstone 900 watt
Cooling
Thermalright Ultra 120 Extreme
Case
Silverstone Raven (1st edition)
Operating System
Windows 10 Spy Delux
Monitor
Xstar 27" PLS 1440p
Keyboard
Wireless ergonomic
Mouse
Logisys Wireless trackball
▲ hide details ▲
PhotonFanatic is offline  
post #15 of 44 (permalink) Old 10-24-2018, 09:28 AM
New to Overclock.net
 
xToaDx's Avatar
 
Join Date: Jul 2008
Posts: 3,015
Rep: 276 (Unique: 225)
My experience is with C, not Python but here's my shot at an explanation.

In the code above, you are telling the 'print' function to expect 2 'integer' data types as the input.
%f is float, %d is integer, %s is string.

When you see numbers inside the format specifier (%4.2d or whatever) generally they will modify the output to be easier to read. In C, %4.2d would print an integer with a minimum field width of 4 characters, and 2 decimal places. %6.3f is a float, 6 character field width, 3 decimal places.

Here is a link to some good documentation:
https://python-reference.readthedocs...ormatting.html

At the top, you can see the syntax is:
%[key][flags][width][.precision][length type]conversion type

So if you wanted to print an integer out to 6 decimal places, you could just type %.6d

xToaDx is offline  
post #16 of 44 (permalink) Old 10-25-2018, 01:22 AM - Thread Starter
New to Overclock.net
 
PhotonFanatic's Avatar
 
Join Date: Dec 2009
Posts: 1,832
Rep: 18 (Unique: 17)
When it comes to %:

Any idea as to why f is for float, and s is for string, but d is for integer? Seems like it would have been i.

"Executing execution.exe"
Bang for the buck
(12 items)
CPU
3570k @ 4.6Ghz @1.35v
Motherboard
Gigabyte Z77x UD3H
GPU
evga GTX 970 SSC
RAM
16Gb Gskill Ripjaws 2133Mhz @ 9-11-10-28
Hard Drive
Samsung Evo 850
Power Supply
Silverstone 900 watt
Cooling
Thermalright Ultra 120 Extreme
Case
Silverstone Raven (1st edition)
Operating System
Windows 10 Spy Delux
Monitor
Xstar 27" PLS 1440p
Keyboard
Wireless ergonomic
Mouse
Logisys Wireless trackball
▲ hide details ▲
PhotonFanatic is offline  
post #17 of 44 (permalink) Old 10-25-2018, 03:22 AM - Thread Starter
New to Overclock.net
 
PhotonFanatic's Avatar
 
Join Date: Dec 2009
Posts: 1,832
Rep: 18 (Unique: 17)
And now this! Today my python lessons said:

Code:
# Ask Python to print sqrt(25) on line 3.

print sqrt(5)

Then it says:

Did you see that? Python said: NameError: name 'sqrt' is not defined. Python doesn't know what square roots are—yet.

Its talking about importing modules but instead of "NameError" shouldn't it be "Variableerror"?

"Executing execution.exe"
Bang for the buck
(12 items)
CPU
3570k @ 4.6Ghz @1.35v
Motherboard
Gigabyte Z77x UD3H
GPU
evga GTX 970 SSC
RAM
16Gb Gskill Ripjaws 2133Mhz @ 9-11-10-28
Hard Drive
Samsung Evo 850
Power Supply
Silverstone 900 watt
Cooling
Thermalright Ultra 120 Extreme
Case
Silverstone Raven (1st edition)
Operating System
Windows 10 Spy Delux
Monitor
Xstar 27" PLS 1440p
Keyboard
Wireless ergonomic
Mouse
Logisys Wireless trackball
▲ hide details ▲
PhotonFanatic is offline  
post #18 of 44 (permalink) Old 10-25-2018, 05:36 AM
New to Overclock.net
 
deepor's Avatar
 
Join Date: Feb 2013
Posts: 4,713
Rep: 472 (Unique: 318)
Quote: Originally Posted by PhotonFanatic View Post
When it comes to %:

Any idea as to why f is for float, and s is for string, but d is for integer? Seems like it would have been i.
I'm thinking "d" is supposed to mean decimal. There's also %x to print an integer as a hexadecimal number and a %o as well:

Code:
>>> number = 1234
>>> print "decimal: %d, hex: %x, octal: %o" % (number, number, number)
decimal: 1234, hex: 4d2, octal: 2322
deepor is offline  
post #19 of 44 (permalink) Old 10-25-2018, 01:28 PM
New to Overclock.net
 
xToaDx's Avatar
 
Join Date: Jul 2008
Posts: 3,015
Rep: 276 (Unique: 225)
Quote: Originally Posted by PhotonFanatic View Post
And now this! Today my python lessons said:

Code:
# Ask Python to print sqrt(25) on line 3.

print sqrt(5)

Then it says:

Did you see that? Python said: NameError: name 'sqrt' is not defined. Python doesn't know what square roots are—yet.

Its talking about importing modules but instead of "NameError" shouldn't it be "Variableerror"?
Integer is %d rather than %i because like Deepor said: d stands for decimal (base 10). You could also have an integer of base 16 (hexadecimal) or base 8 (octal).

Sqrt() is not a variable, it is a function. Basically python is telling you that it doesn’t know what to do with the number 5, since “sqrt” is undefined at this point.

In C, if you want to use math functions, you type something like “#include <math.h>” which defines math functions so that you can then “call” them inside your program.
I’m guessing Python does something similar. In C, math.h is called a “header” but it sounds like Python calls them “modules.”

Think about how you would handle the square root of 5 if I asked you to compute it by hand (no calculator). What would you do? I’m assuming you would need to look up the method. I would too. (“Newton’s Method” is a popular one)
Python needs to look it up too, in a module.

Just as general advice, I would highly highly suggest taking math courses along side programming. The best programmers are typically very good at math.

xToaDx is offline  
post #20 of 44 (permalink) Old 10-25-2018, 01:35 PM
New to Overclock.net
 
xToaDx's Avatar
 
Join Date: Jul 2008
Posts: 3,015
Rep: 276 (Unique: 225)
Oh, and the probable reason for Python NOT including the math functions by default is that it will reduce compile time.
If you had no math functions in your code, why would you want to bother with telling Python what the definition of a square root is? It would be a waste of time.

Some languages, like MATLab, include the math functions by default because the assumption is that you are using that specific language for math related things.

xToaDx is offline  
Reply

Quick Reply
Message:
Options

Register Now

In order to be able to post messages on the Overclock.net - An Overclocking Community forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.
User Name:
If you do not want to register, fill this field only and the name will be used as user name for your post.
Password
Please enter a password for your user account. Note that passwords are case-sensitive.
Password:
Confirm Password:
Email Address
Please enter a valid email address for yourself.
Email Address:

Log-in



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page


Forum Jump: 

Posting Rules  
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off