# Python: Basics

## Numbers

To type in a whole number known as an integer into Python we can just use the number for example:

Python

This will return the value


1



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:

Python

2.03



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:

$\displaystyle 2\times 10\times 10\times 10$

Which can be simplified as:

$\displaystyle 2\times {{10}^{3}}$

For convenience in Python we use the letter e in place of $\displaystyle \times 10$ and do not superscript the power:

Python

2,000



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:

$\displaystyle 4\times \frac{1}{{10}}\times \frac{1}{{10}}\times \frac{1}{{10}}$

Which can be simplified as:

$\displaystyle 4\times {{10}^{{-3}}}$

Python

0.004



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:

Python

300000000.0



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:

The number:

299,792,458

Can be written as:

2.99792458×100 000 000

To the left hand side we have the mantissa and to the right hand side we have a mantissa of 1 followed by, 8 zeros. This gives the same exponent of 8 (as expected as we are just rounding the number above) and therefore we can write this number as:

Python

299792458.0



The number:

0.000 000 000 000 000 000 000 000 000 000 663

Can be written as:

6.63 × 0.000 000 000 000 000 000 000 000 000 000 001

The number 1 is 34th place after the decimal point therefore the mantissa is 6.63 and the exponent is -34:

Python

6.63e-34



## Basic Operations

We can add a second number to an existing first number using the + operator for example 2 plus 4:

Python

6



We can subtract a second number from the first number using the – operator. For example 4 minus 2:

Python

2



We can multiple two numbers together using the * operator. For example 4 times 2:

Python

8



And we can divide the first number by the second number using the / operator.

Python

4


Python

8



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.

Python

10



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.

Python

12



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.

## Functions

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:

Python

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:

Python

int



And we get told that we have an integer (whole number).

Python

float



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:

Python

float


Python

float



The addition of an integer and another integer will return an integer:

Python

int



However addition of a float and integer will return a float:

Python

float



## Assigning Variables

Numbers can be assigned to Variables. For instance we may assign the value of 2 to a and the value of 1 to b:

Python
Python

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:

Python

We can therefore use it with a single argument to print the value of assigned variables to screen for example:

Python

a=2


Python

b=1



It is also possible to input multiple variables into the print function for example:

Python

2 1



Arithmatic operations can be carried out as above with the assigned variables. For instance:

Python

2



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:

Python

Once again, no output is displayed, to show the output of this new variable we can once again use:

Python

c=2



Note variables are case-sensitive. If we type in:

Python

NameError: name 'C' is not defined



We will get the error message above.

## Inputting Text

To create a string of characters (called a string) we need to surround our text in either single or double quotation marks. For example:

Python

'Philip'



Or:

Python

'Philip'



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:

Python

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:

Python

SyntaxError: can't assign to literal


Python

SyntaxError: invalid syntax



However the variable name:

Python

Is allowed.

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:

Python

Philip



We can also check the type of the Variable by using the function type and input the Variable Name as an input argument:

Python

str



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:

Python

Sarah



If we type in the following:

Python

90
90



These appear to be the same however if we type in:

Python

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:

Python

str
int



## Boolean Values

We can also define a variable as True or False. Using:

Python

This once again should not be confused with:

Python

Once again we can use the print functions and the values may appear identical:

Python

True
True



However the differences in both can be seen if we lookup their type:

Python

bool
str



If Arithmetic is done with Boolean, they are converted into int with 'True'=1 and 'False'=0 for example:

Python

2
int


Python

0
int