Help Python Command
The help utility will show you detailed information on any topic you want. I would highly recommend that you try exploring the help() for a while. When you type help () in the python prompt and press ENTER key, it will change from >>> to help> prompt. If you want to quit the help prompt just press CTRL + C (or) press CTRL + Z and press ENTER key.
You can start using python interpreter as a calculator by feeding arithmetic calculations inline. Trying entering the below examples:
>>> 6 + 7
>>> 5.3 + 8.9
>>> 150 * 66
>>> 2 ** 3
>>> 10 % 9
>>> 99 / 33
As you can see the result of a division always results in a floating point number. If you want to discard the remainder then you can use double forward slash //
>>> 6 // 3
You can use operators like <,>, <=, >=, == and != to compare strings or numbers which would give you a boolean result that is either True or False.
>>> 'a' != 'A'
>>> 'a' == 'A'
>>> 5 < 3
You can either represent string using single quotes or using double quotes
>>> 'This is a single quote text'
‘This is a single quote text’
>>> "This is a double quote text"
‘This is a double quote text’
If you want to concatenate two or more strings together then use the ‘+’ operator as shown below
>>> "Text A " + "Text B"
‘Text A Text B’
You can also specify something like the below,
>>> 2 * 'tick' + 'tock'
2 times ‘tick’ to be concatenated to the string ‘tock’
If you want to place double quotes within a string that is already enclosed by double quotes, then you can use the escape character backslash before the double quotes.
>>> "Text \"A\" " + "Text B"
‘Text “A” Text B’
In the above example we had to enclose the “A” within double quotes so we had to use the escape character backslash. The same method has to be followed if you need to enclose a single quotes string. You can also rewrite the above command as shown below which doesn’t require the escape character.
>>> 'Text "A" ' + 'Text B'
‘Text “A” Text B’
If you would like to print some text or result of an operation then you can use the print command.
>>> print("Hello World!!!");
Note: If you are using Python version 2.x then you may not use the open and closing parenthesis of the print statement, it would just be print “Hello World!!!”
There could be times where you would like to print text that spans multiples lines in such cases you can make use of the triple double quotes (or) triple single quotes.
>>> print(""" This is a
multiple line text, that is created
to show you that it actually works!!! """)
If you are using a file path in a print statement then there could be chances that it contains escape sequences like \t (TAB), \n (Newline), \r (Carriage Return) etc.
Would print as shown below:
Quite messy isn’t it? there are actually two ways in which you can handle this,
1) Specify a string as raw string by adding ‘r’ before the string.
2) Use double backslash
Comments are usually used to explain something in the code so that someone who looks into your code can understand it.
Single line comments – use # to specify single line comments.
>>> # This is a single line comment
Multi-line comments – use triple quotes “”” to specify comments.
all the text that goes in here are just comments, nothing serious
Note: If you use the same triple quotes within the print statement then it would print it.