Some beginner's questions about python

Niilesh

Padawan
I have started leaning python(3.2) recently. I am using 'A Byte of python'(ver. 1.92) to learn.I selected this book because i have no programming experience(but i have seen about 15 video tutorials of C++ so i know about variables and if statement). It's a good book but i feel is less compressive. Are there any better books available?

Some more questions :
Q.Is IDLE is the right choice for a beginner like me?
Q.How create executable python programs?
Q.Whenever i run script by pressing F5 in IDLE, it restarts the python shell then run it. Is it normal?(by restarting i do not mean that it again runs it in another window)
Q.How to run python programs directly from python shell?(by knowing its name and directory)
 

coolpcguy

Resistance is Futile.
Q.Is IDLE is the right choice for a beginner like me?

IDLE will do fine.

Q.Is IDLE is the right choice for a beginner like me?
Which OS? If it's Windows there's Py2Exe

Is it normal?
Not sure I understand this, it runs in the same shell. Then again, I'm on Linux

Q.How to run python programs directly from python shell?(by knowing its name and directory)
Code:
execfile('filename')
 
Last edited:

coolpcguy

Resistance is Futile.
^ Your link is broken. Seems like imgur is having problems.
(yea, Markdown to BBCode trips me always)
 
OP
Niilesh

Niilesh

Padawan
^ I am able to see the pic.
Another question
Q.what's the use of specifying filename and directory in the start of the source code?
like this:
Code:
#!/usr/bin/python
# Filename: str_format.py

Also
Are there any better books available?
 

coolpcguy

Resistance is Futile.
^ I am able to see the pic.
Yeah, it's fine now, imgur was down earlier,

Code:
# Filename: str_format.py
That's a comment, part of documentation.

Code:
#!/usr/bin/python

That's a she-bang or a hashbang. It tells the shell what interpreter must be loaded to parse the remaining lines.

*stackoverflow.com/q/3009192/92837 explains it better.


Are there any better books available?

For a new comer, I'd recommend Zed Shaw's Learn Python The Hard Way.

Learn Python The Hard Way, 2nd Edition — Learn Python The Hard Way, 2nd Edition


Ed: As for the picture, yes that's standard.
 
OP
Niilesh

Niilesh

Padawan
Yeah, it's fine now, imgur was down earlier,
Code:
# Filename: str_format.py
That's a comment, part of documentation.
I wanted to know whats the point of telling the filename(to fellow readers) ?

Code:
#!/usr/bin/python
That's a she-bang or a hashbang. It tells the shell what interpreter must be loaded to parse the remaining lines.
*stackoverflow.com/q/3009192/92837 explains it better.
Didn't understand its need will have to read the article on wikipedia

For a new comer, I'd recommend Zed Shaw's Learn Python The Hard Way.
Learn Python The Hard Way, 2nd Edition — Learn Python The Hard Way, 2nd Edition
Its not updated for 3.0 :(
Worth learning 2.0 after the launch of 3.0 in 2010?

Ed: As for the picture, yes that's standard.
Ok

Hey can you explain this topic :- Python en:First Steps - Notes
 
Last edited:

krishnandu.sarkar

Simply a DIGITian
Staff member
Well let's put it like this.

That she-bang has no use in Windows but it's of much use in Linux.

#!/usr/bin/python, asks the system to run the script using that program. Generally programs (read apps for windows) are generally present in /usr/bin/

So python is there in /usr/bin/. So to run python script we need to do...

python file_name.py (in the terminal)

But if we enable the executable bit of that file, and declare that shebang we don't need to run it like that, instead running it by ./file_name.py will execute it, becasue that she-bang already asks the system to execute the file using /usr/bin/python program.

Hope that helps.
 

prabhu.wali

prabhu.wali
udacity is a very good source to start learning python Udacity - Free Classes. Awesome Instructors. Inspiring Community.
 

coolpcguy

Resistance is Futile.
You should start with 2.x series, moving to 3.x later isn't that tough. Start with Zerd Shaw's book. Dive into Python IMO is more of a reference for those who know how to do things and want to learn further.

Didn't understand its need will have to read the article on wikipedia

tl;dr: the file line tells the Linux shell that the file mentioned after #! must be used to interpret the source code after the line.

Krishnandu.sarkar has explained it fairly well.

Hey can you explain this topic :- Python en:First Steps - Notes
What part of it?
Code:
#!/usr/bin/env python
This one?
This asks the shell what's the location of python binary and uses that location to run it.
in comparison to this:

Code:
#!/usr/bin/python
Where python is run directly from /usr/bin path. The former is preferred because of pyhton (or any other, for that matter) is installed in a location other than the standard, /usr/bin/env will provide the location of python & your script will run, as opposed to the second condition where it'll just fail
 
OP
Niilesh

Niilesh

Padawan
Well let's put it like this.

That she-bang has no use in Windows but it's of much use in Linux.

#!/usr/bin/python, asks the system to run the script using that program. Generally programs (read apps for windows) are generally present in /usr/bin/

So python is there in /usr/bin/. So to run python script we need to do...

python file_name.py (in the terminal)

But if we enable the executable bit of that file, and declare that shebang we don't need to run it like that, instead running it by ./file_name.py will execute it, becasue that she-bang already asks the system to execute the file using /usr/bin/python program.

Hope that helps.
OK
Thanx.. :D

udacity is a very good source to start learning python Udacity - Free Classes. Awesome Instructors. Inspiring Community.
I was thinking to join it but its cource had reached the 6th or 7th week so it was pretty late to join..

You should start with 2.x series, moving to 3.x later isn't that tough. Start with Zerd Shaw's book. Dive into Python IMO is more of a reference for those who know how to do things and want to learn further.
Ok..
It says to use "gedit', should i use it? or use IDLE or Notepad++?
What part of it?
I first wanted to understand the whole "Executable Python Programs" topic
But now i understand the first part. Explain this :
Code:
   $ echo $PATH
   /usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/swaroop/bin
   $ cp helloworld.py /home/swaroop/bin/helloworld
   $ helloworld
   Hello World
 
Last edited:

Liverpool_fan

Sami Hyypiä, LFC legend
OK
I was thinking to join it but its cource had reached the 6th or 7th week so it was pretty late to join..
Just dive in. No worries.

It says to use "gedit', should i use it? or use IDLE or Notepad++?
Any text editor you like.
I first wanted to understand the whole "Executable Python Programs" topic
But now i understand the first path. Explain this :
Code:
   $ echo $PATH
   /usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/swaroop/bin
   $ cp helloworld.py /home/swaroop/bin/helloworld
   $ helloworld
   Hello World
Your $HOME/bin is usually in your path. If you move your script to ~/bin, then you can use your script as an ordinary command without specifying the path.
 
OP
Niilesh

Niilesh

Padawan
Your $HOME/bin is usually in your path. If you move your script to ~/bin, then you can use your script as an ordinary command without specifying the path.

So
Code:
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/swaroop/bin[B] >This specifies the path of file[/B]
$ cp helloworld.py /home/swaroop/bin/helloworld[B] >this copies it to bin directry[/B]
$ helloworld[B] >This runs it[/B]
Hello World
Right?
 

Liverpool_fan

Sami Hyypiä, LFC legend
The first outputs the directory paths which are searched for scripts/programs. Apart from that you're correct.
 

coolpcguy

Resistance is Futile.
It says to use "gedit', should i use it? or use IDLE or Notepad++?
Whatever you're comfortable with.

Code:
$ echo $PATH

$PATH is the environment variable which mentions the directories the shell will look the file to exist to be able to run without having to mention the full path.

Code:
/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/swaroop/bin

So the shell will look for directories

- /usr/local/bin
- /usr/bin
- /bin:/usr/X11R6/bin
- /home/swaroop/bin

In other words, if an executable is located in any of the above directories, you don't have to provide to the full path to the executable.

Code:
   $ cp helloworld.py /home/swaroop/bin/helloworld

Copies helloworld.py to the helloworld file in /home/swaroop/bin/ directory.

Code:
   $ helloworld
Runs the file.
 
OP
Niilesh

Niilesh

Padawan
What if multiple instances of the file is present in different directories? Will it run all of them. and How will it copy the files with same extension(and name) to same folder?

Also PATH is a command that is registered in linux terminal or the python shell?
 

skeletor

Chosen of the Omnissiah
What if multiple instances of the file is present in different directories? Will it run all of them. and How will it copy the files with same extension(and name) to same folder?
If /usr/local/bin is before $HOME/usr/bin when you output $PATH and the executable with the same name exists in both, then /usr/local/bin will get priority as it comes first. Just a hypothetical example.

Also PATH is a command that is registered in linux terminal or the python shell?
It's an environment "variable" used by the OS.

Check out %TEMP%, %PATH% and the likes in Windows.
 
OP
Niilesh

Niilesh

Padawan
If /usr/local/bin is before $HOME/usr/bin when you output $PATH and the executable with the same name exists in both, then /usr/local/bin will get priority as it comes first. Just a hypothetical example.
OK

It's an environment "variable" used by the OS.

Check out %TEMP%, %PATH% and the likes in Windows.
OK, Thanks for the example
 

$$Lionking$$

In the zone
Hey guys - I also have a noob query..

I'm thinking of learning a scripting language good enuf to put it up on my resume... and I was hoping to learn python..

But I already have some experience with haskell so what shud I do??! Haskell Or python?? Also why is that every developer/programmer who is using Linux knows python?! :O I just don't get it!!!

TIA.
 

krishnandu.sarkar

Simply a DIGITian
Staff member
Well python is the new generation language. It's really easy to grasp for newbies even with no programming background experience.

Also it's cross-platforrm and scalability feature made it more popular.

Sent from my LG-P500 using Tapatalk 2 Beta-5
 
Top Bottom