Python beginner

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Python beginner

Postby cbuffer » Sat May 15, 2010 11:47 pm

Can someone point me to an explanation of the significance of the L in line 2 please?

>>> pow(2,31)
2147483648L
>>> print pow(2,31)
2147483648

TIA

Ken
cbuffer
 
Posts: 67
Joined: Thu Nov 01, 2007 12:04 am

Postby graemef » Sun May 16, 2010 2:32 am

The L indicates that the data type is a Long integer. The second version which prints the value converts the numeric to a character string and the data type is not shown.
graemef
 
Posts: 8
Joined: Wed Apr 07, 2010 8:55 am

Postby LeeNukes » Sun May 16, 2010 8:15 am

While on the theme of Python, why are indents such an issue? I used to think they were just to make it easy for people to read, but it stops a program working if they are slightly wrong.
User avatar
LeeNukes
LXF regular
 
Posts: 954
Joined: Sun Jun 21, 2009 8:11 pm
Location: At the bar

Postby ollie » Sun May 16, 2010 10:15 am

Dive into Python wrote:Python functions have no explicit begin or end, and no curly braces to mark where the function code starts and stops. The only delimiter is a colon (:) and the indentation of the code itself.


Ref: http://book.opensourceproject.org.cn/lamp/python/diveintopython/index.html

Indenting is a structural requirement of Python syntax.
User avatar
ollie
Moderator
 
Posts: 2749
Joined: Mon Jul 25, 2005 11:26 am
Location: Bathurst NSW Australia

Postby nelz » Sun May 16, 2010 11:39 am

LeeNukes wrote:While on the theme of Python, why are indents such an issue? I used to think they were just to make it easy for people to read, but it stops a program working if they are slightly wrong.


Because indentation is part of the syntax, it does the job of braces in most other languages. You'd expect a syntax error from a missing brace in them, you get the same from a missing tab in Python, for exactly the same reasons.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8490
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby cbuffer » Sun May 16, 2010 4:18 pm

graemef wrote:The L indicates that the data type is a Long integer. The second version which prints the value converts the numeric to a character string and the data type is not shown.


Thank you graemef, I really should have worked that out for myself.

Ken
cbuffer
 
Posts: 67
Joined: Thu Nov 01, 2007 12:04 am

Postby LeeNukes » Sun May 16, 2010 5:39 pm

nelz wrote:
LeeNukes wrote:While on the theme of Python, why are indents such an issue? I used to think they were just to make it easy for people to read, but it stops a program working if they are slightly wrong.


Because indentation is part of the syntax, it does the job of braces in most other languages. You'd expect a syntax error from a missing brace in them, you get the same from a missing tab in Python, for exactly the same reasons.


at least braces are visible though. Trying to work out which indent needs to line up with which isn't straight forward.
User avatar
LeeNukes
LXF regular
 
Posts: 954
Joined: Sun Jun 21, 2009 8:11 pm
Location: At the bar

Postby JPGargoyle » Sun May 16, 2010 5:50 pm

LeeNukes wrote:at least braces are visible though. Trying to work out which indent needs to line up with which isn't straight forward.


How can you say that?

If you don't know what must be lined up, then you don't know what you are programming.
If you are writing a python (or any other) program, it's you who know it's logical structure, so for example you should know all the code that must be executed inside an if statement for your program to work. If that block of code must be inside braces, or equally indented, it's exactly the same.

Best regards
User avatar
JPGargoyle
 
Posts: 20
Joined: Thu Jul 28, 2005 11:29 pm

Postby LeeNukes » Sun May 16, 2010 6:04 pm

JPGargoyle wrote:
LeeNukes wrote:at least braces are visible though. Trying to work out which indent needs to line up with which isn't straight forward.


How can you say that?

If you don't know what must be lined up, then you don't know what you are programming.
If you are writing a python (or any other) program, it's you who know it's logical structure, so for example you should know all the code that must be executed inside an if statement for your program to work. If that block of code must be inside braces, or equally indented, it's exactly the same.

Best regards


This was someone elses code I was trying to get to work. I sorted it in the end, but I had to mess with the indents,
User avatar
LeeNukes
LXF regular
 
Posts: 954
Joined: Sun Jun 21, 2009 8:11 pm
Location: At the bar

Postby JPGargoyle » Sun May 16, 2010 6:14 pm

LeeNukes wrote:This was someone elses code I was trying to get to work. I sorted it in the end, but I had to mess with the indents,


Ah, that's the major point on python's indenting: posting code online, copy-pasting it, leaving to situations where white-spacing and formatting is lost. But that's no reason to change one of the (IMHO) best features of python.

Best regards
User avatar
JPGargoyle
 
Posts: 20
Joined: Thu Jul 28, 2005 11:29 pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests