Basics Of Python
Comments
Comments are any text to the right of the
#symbol and is very useful to the reader of the program. For example:
print("hello world") # Print is a function and here uses
#double quote
#Note that print is a function and uses single quotes
print('hello world')
Use as many useful comments as you can use in your program but remember to keep them short and discriptive and use them to:
- explain assumptions
- explain important decisions
- explain important details
- explain problems you’re trying to solve
This is useful for readers of your program so that they can easily understand what the program is doing. Remember, that person can be you after months.
Literal Constants
An example of a literal constant is a number like 5, 1.23, or a string like ‘This is a string’ or “It’s a string”.
NOTE: You can use single quotes or double quotes while dealing with strings.
The number 5 always represents itself and nothing else - it is a constant because its value cannot be changed. Hence, all these are referred to as literal constants.
Numbers
Numbers are mainly of two types - integers and floats.
An example of an integer is 2 which is just a whole number.
Examples of floating point numbers are 3.23 and 52.3E-4.
NOTE: The E notation indicates powers of 10. In this case, 52.3E-4 means 52.3 * 10^-4^.
Note for Programmers with Another Programming Language Background
There is no separate long type. The int type can be an integer of any size.
Strings
A string is a sequence of characters. Strings are basically just a collection of words.
You will be using strings in almost every Python program that you write, so understand them carefully
Single Quote
You can specify strings using single quotes
Example ‘What is your name?’.
Double Quotes
Strings in double quotes work exactly the same way as strings in single quotes.
Example: “What’s your name?”.
Triple Quotes
You can specify multi-line strings using triple quotes - (""" or ‘’’). You can use single quotes and double quotes freely within the triple quotes.
Example:
’’'This is a multi-line string. This is the first line. This is the second
line. “What’s your name?,” I asked. He said “John” ‘’'
Strings Are Immutable
This means that once you have created a string, you cannot change it.