Ques 1 : What does #!/usr/bin/python mean?
You've probably already seen one of the following lines:
#!/bin/sh
#!/usr/bin/python
#!/usr/bin/python3
#!/usr/bin/env python
#!/usr/bin/perl
#!/usr/bin/php
#!/usr/bin/ruby
This is a shebang. It's a directive for your command line interpreter how it should execute a script.
For example, you have a file with this content:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
print(sys.version_info)
Now you can execute it via python3 yourFile.py. But alternatively, you can make it executable and simply type ./yourFile.py
By the way, you should use #!/usr/bin/env python for some reasons.
Ques 2 : Why is '#!/usr/bin/env python' supposedly more correct than just '#!/usr/bin/python'?
When do you
#!/usr/local/bin/python
You are specifying the location to the python executable in your machine, that rest of the script needs to be interpreted with.
You are pointing to python is located at /usr/local/bin/python
Consider the possiblities that in a different machine, python may be installed at /usr/bin/python or /bin/python in those cases, the above #! will fail.
For those cases, we get to call the env executable with argument which will determine the arguments path by searching in the $PATH and use it correctly.
Thus,
#/usr/bin/env python
Will figure out the correct location of python ( /usr/bin/python or /bin/python from $PATH) and make that as the interpreter for rest of the script.
- ( env is almost always located in /usr/bin/ so one need not worry what is env is not present at /usr/bin)
Ques 3 : How do I get my country's language characters into my file?
Just add at top of you file # -*- coding: utf-8 -*-
Ques 4 : If # is for comments, then how come # -*- coding: utf-8 -*- works?
Python still ignores that as code, but it's used as a kind of "hack" or workaround for problems with setting and detecting the format of a file. You also find a similar kind of comment for editor settings.
Ques 5 : How do I comment out multiple lines?
Put a # in front of each one.
or use tripple quote as following:
'''
This is the multiple line
comments
'''
Ques 6 : How to get results in decimal points using '/'?
use either following expression:
10/3.0 or 10.0/3 or 10.0/3.0
Ques 7 : What is the difference between %r and %s ?
Use the %r for debugging, since it displays the "raw" data of the variable, but the others are used for displaying to users.
Ques 8 : what will do the folowing code ?
print "." * 10
it will print "." 10 times.
Ques 9 : what will be output of following script?
#!/usr/bin/python
print "mukesh"
print "kumar"
############
print "mukesh",
print "kumar"
Ques 10 : I tried putting Chinese (or some other non-ASCII characters) into these strings, but %r prints out weird symbols.
Use %s to print that instead and it'll work.
Ques 11 : Why do the \n newlines not work when I use %r?
That's how %r formatting works; it prints it the way you wrote it (or close to it). It's the "raw" format for debugging.
Ques 12 : What's the difference between input() and raw_input()?
The input() function will try to convert things you enter as if they were Python code, but it has security problems so you should avoid it.
Ques 13 : When my strings print out there's a u in front of them, as in u'35'.
That's how Python tells you that the string is Unicode. Use a %s format instead and you'll see it printed like normal.
Ques 14 : pydoc command ?
In Terminal where you normally run python to run your scripts, type pydoc raw_input. Read what it says. If you're on Windows try python -m pydoc raw_input instead.
exp: pydoc sys
Ques 15 : output of the command print "How old are you?" , raw_input()
>>> print "How old are you?" , raw_input()
How old are you? 12
12
>>>
Ques 16 : Are the command line arguments strings?
Yes, they come in as strings, even if you typed numbers on the command line. Use int() to convert them just like with int(raw_input()).
Ques 17 : What does mean the following file reading and writing related commmands.
close -- Closes the file. Like File->Save.. in your editor.
read -- Reads the contents of the file. You can assign the result to a variable.
readline -- Reads just one line of a text file.
truncate -- Empties the file. Watch out if you care about the file.
write('stuff') -- Writes "stuff" to the file.
Ques 18 : What does 'w' mean?
It's really just a string with a character in it for the kind of mode for the file. If you use 'w' then you're saying "open this file in 'write' mode," thus the 'w' character. There's also 'r' for "read," 'a' for append, and modifiers on these.'
Ques 19 : What modifiers to the file modes can I use?
The most important one to know for now is the + modifier, so you can do 'w+', 'r+', and 'a+'. This will open the file in both read and write mode, and depending on the character use position the file in different ways.
Ques 20 : Does just doing open(filename) open it in 'r' (read) mode?
Yes, that's the default for the open() function.
You've probably already seen one of the following lines:
#!/bin/sh
#!/usr/bin/python
#!/usr/bin/python3
#!/usr/bin/env python
#!/usr/bin/perl
#!/usr/bin/php
#!/usr/bin/ruby
This is a shebang. It's a directive for your command line interpreter how it should execute a script.
For example, you have a file with this content:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
print(sys.version_info)
Now you can execute it via python3 yourFile.py. But alternatively, you can make it executable and simply type ./yourFile.py
By the way, you should use #!/usr/bin/env python for some reasons.
Ques 2 : Why is '#!/usr/bin/env python' supposedly more correct than just '#!/usr/bin/python'?
When do you
#!/usr/local/bin/python
You are specifying the location to the python executable in your machine, that rest of the script needs to be interpreted with.
You are pointing to python is located at /usr/local/bin/python
Consider the possiblities that in a different machine, python may be installed at /usr/bin/python or /bin/python in those cases, the above #! will fail.
For those cases, we get to call the env executable with argument which will determine the arguments path by searching in the $PATH and use it correctly.
Thus,
#/usr/bin/env python
Will figure out the correct location of python ( /usr/bin/python or /bin/python from $PATH) and make that as the interpreter for rest of the script.
- ( env is almost always located in /usr/bin/ so one need not worry what is env is not present at /usr/bin)
Ques 3 : How do I get my country's language characters into my file?
Just add at top of you file # -*- coding: utf-8 -*-
Ques 4 : If # is for comments, then how come # -*- coding: utf-8 -*- works?
Python still ignores that as code, but it's used as a kind of "hack" or workaround for problems with setting and detecting the format of a file. You also find a similar kind of comment for editor settings.
Ques 5 : How do I comment out multiple lines?
Put a # in front of each one.
or use tripple quote as following:
'''
This is the multiple line
comments
'''
Ques 6 : How to get results in decimal points using '/'?
use either following expression:
10/3.0 or 10.0/3 or 10.0/3.0
Ques 7 : What is the difference between %r and %s ?
Use the %r for debugging, since it displays the "raw" data of the variable, but the others are used for displaying to users.
Ques 8 : what will do the folowing code ?
print "." * 10
it will print "." 10 times.
Ques 9 : what will be output of following script?
#!/usr/bin/python
print "mukesh"
print "kumar"
############
print "mukesh",
print "kumar"
Ques 10 : I tried putting Chinese (or some other non-ASCII characters) into these strings, but %r prints out weird symbols.
Use %s to print that instead and it'll work.
Ques 11 : Why do the \n newlines not work when I use %r?
That's how %r formatting works; it prints it the way you wrote it (or close to it). It's the "raw" format for debugging.
Ques 12 : What's the difference between input() and raw_input()?
The input() function will try to convert things you enter as if they were Python code, but it has security problems so you should avoid it.
Ques 13 : When my strings print out there's a u in front of them, as in u'35'.
That's how Python tells you that the string is Unicode. Use a %s format instead and you'll see it printed like normal.
Ques 14 : pydoc command ?
In Terminal where you normally run python to run your scripts, type pydoc raw_input. Read what it says. If you're on Windows try python -m pydoc raw_input instead.
exp: pydoc sys
Ques 15 : output of the command print "How old are you?" , raw_input()
>>> print "How old are you?" , raw_input()
How old are you? 12
12
>>>
Ques 16 : Are the command line arguments strings?
Yes, they come in as strings, even if you typed numbers on the command line. Use int() to convert them just like with int(raw_input()).
Ques 17 : What does mean the following file reading and writing related commmands.
close -- Closes the file. Like File->Save.. in your editor.
read -- Reads the contents of the file. You can assign the result to a variable.
readline -- Reads just one line of a text file.
truncate -- Empties the file. Watch out if you care about the file.
write('stuff') -- Writes "stuff" to the file.
Ques 18 : What does 'w' mean?
It's really just a string with a character in it for the kind of mode for the file. If you use 'w' then you're saying "open this file in 'write' mode," thus the 'w' character. There's also 'r' for "read," 'a' for append, and modifiers on these.'
Ques 19 : What modifiers to the file modes can I use?
The most important one to know for now is the + modifier, so you can do 'w+', 'r+', and 'a+'. This will open the file in both read and write mode, and depending on the character use position the file in different ways.
Ques 20 : Does just doing open(filename) open it in 'r' (read) mode?
Yes, that's the default for the open() function.
No comments:
Post a Comment