To type in a whole number known as an integer into Python we can just use the number for example:
This will return the value
We can also specify a number with a decimal point using the form. This type of number is known as a floating point number or a float:
In mathematics and physics we are often dealing with either very small numbers or very large numbers. For convenience these can be expressed in exponential form as a floating point number. Because we humans are born with 10 fingers, we typically use the the power of 10, called decimalto count with. The number 2 000 for example can be expressed as:
Which can be simplified as:
For convenience in Python we use the letter e in place of and do not superscript the power:
In this case the mantissa, the number before the exponent is +2 and the exponent, the number after e is +3. Likewise the number 0.004 can be expressed as:
Which can be simplified as:
In this case the mantissa, the number before the exponent is +4 and the exponent, the number after e is -3. The speed of light in air is approximately 300 000 000 metres per second, here the Mantissa is +3 and the exponent is +8 (there are 8 zeros following the mantissa of 3). This value can be written as:
Practice: A more exact value is 299,792,458 metres per second write this out as an exponent. The Planck Constant is 0.000 000 000 000 000 000 000 000 000 000 663 Joule seconds write this out as an exponent:
We can add a second number to an existing first number using the + operator for example 2 plus 4:
We can subtract a second number from the first number using the – operator. For example 4 minus 2:
We can multiple two numbers together using the * operator. For example 4 times 2:
And we can divide the first number by the second number using the / operator.
We can use parenthesis to specify the order of an operation. In generanl we follow the arithmetic rules BODMAS – Brackets (or parenthesis), Orders, Division, Multiplication, Addition and Subtraction.
In the above example we have an Addition and a Multiplication. The Multiplication takes precedence so we get the intermediate of 2+8 which gives us 10. In contrast below we have a Bracket (or parenthesis), Addition and Multiplication.
The Brackets or parenthesis take precedence, so the addition which is in the parenthesis gets carried out before the multiplication. Thus we get the intermediate value of 6*2 which gives 12.
Python has a large number of functions, these are usually keywords and followed by parenthesis. For example we can use the function type to look at the numbers we typed above. If we just type the function we will get an error:
TypeError: type() takes 1 or 3 arguments
The input argument needs to be placed inside the bracket. Using the examples above we can type in:
And we get told that we have an integer (whole number).
And we get told we have a floating point number (decimal number). We will also get floats if we use input arguments in exponential form:
The addition of an integer and another integer will return an integer:
However addition of a float and integer will return a float:
Numbers can be assigned to Variables. For instance we may assign the value of 2 to a and the value of 1 to b:
Note when we create this assignment there is no output on the screen. Instead the variables are created in the variable editor. If we wish to display these assigned variables on screen we can use the function:
We can therefore use it with a single argument to print the value of assigned variables to screen for example:
It is also possible to input multiple variables into the print function for example:
Arithmatic operations can be carried out as above with the assigned variables. For instance:
In this case a=2 and b=1 and a*b is 2*1 which equals 2. The answer itself can be saved to a variable for example:
Once again, no output is displayed, to show the output of this new variable we can once again use:
Note variables are case-sensitive. If we type in:
NameError: name 'C' is not defined
We will get the error message above.
To create a string of characters (called a string) we need to surround our text in either single or double quotation marks. For example:
If we type in a string without these quotation marks we will instead get an error:
NameError: name 'Philip' is not defined
This is because Python is looking for the Variable Philip when Philip is typed without quotation marks. Because this Variable has not been defined we get the error message above.
We can also assign text to a Variable for example:
By convention we use lower case words separated by an underscore for variable names. Once again no output is displayed because we are performing an assignment and not specifying to show the output.
Here my_name is the name of the Variable and ‘Philip’ is the string contained in the Variable my_name. Note that we can not use spaces or special characters in the Variable Name, however we can use the underscore. We are not allowed to name a variable as a number or use a number as the first character of a Variable Name.
For example, the following two variable names are not allowed:
SyntaxError: can't assign to literal
SyntaxError: invalid syntax
However the variable name:
Once again to display the value of an assigned variable to screen we can use the function print and input the Variable Name as an input argument:
We can also check the type of the Variable by using the function type and input the Variable Name as an input argument:
We see that the type is a string as expected.
It also also possible to type in a string directly as an input argument to the print function:
If we type in the following:
These appear to be the same however if we type in:
TypeError: can only concatenate str (not "int") to str
This error message shows that we cannot add both these values of 90 as one is a strinf and the other is an integer. The difference in both can be confirmed by typing in:
We can also define a variable as True or False. Using:
This once again should not be confused with:
Once again we can use the print functions and the values may appear identical:
However the differences in both can be seen if we lookup their type:
If Arithmetic is done with Boolean, they are converted into int with ‘True’=1 and ‘False’=0 for example: