# Difference between revisions of "Python"

(→Integers and Floats) |
(→Operators) |
||

Line 25: | Line 25: | ||

===== Operators ===== | ===== Operators ===== | ||

+ | {| class="wikitable" style="text-align: left;" | ||

+ | ! Character | ||

+ | ! Operator | ||

+ | ! Example | ||

+ | |- | ||

+ | | + || Addition || <source lang="python">a = 12 + 14.3 # Returns 26.5</source> | ||

+ | |- | ||

+ | | - || Subtraction || <source lang="python">a = 2 - 2.9 # Returns -0.9</source> | ||

+ | |- | ||

+ | | * || Multiplication || <source lang="python">a = 4 * 1.5 # Returns 6</source> | ||

+ | |- | ||

+ | | / || Division || <source lang="python">a = 9 / 3 # Returns 3</source> | ||

+ | |- | ||

+ | | % || Modulus || <source lang="python">a = 7 % 3 # Returns 1</source> || Returns integer the integer <br/> remainder after division | ||

+ | |- | ||

+ | | // || Floor division || <source lang="python">a = 8 // 3 # Returns 2</source> || Rounds down after division | ||

+ | |} | ||

==== Booleans ==== | ==== Booleans ==== |

## Revision as of 08:49, 20 June 2019

## Contents

### Variables

Variables allow you to store and change values based on certain conditions. In many low level languages like C, C++ and Java when declaring a variable it is required that you also specify of what type it will be. This type is then static and will never change. In java for example declaring a variable that will in the future store a number looks like this:

int some_number;

Many scripting languages, like python, are weakly or loosely type. This means that you are not required to asign a type to a variable when you create this. Variable can also easily change types. This makes the language extremely flexible, but it become easy to get confused with what type a variable is.

It is thus recommended that you type variables. An example of typing in python:

`some_number: int`

#### Integers and Floats

Integers and floats are the default number data types in python. Integers are whole numbers, while floats are fraction, or number with decimals. Both integers and floats can be positive or negative.

Assigning a value to an integer or float looks as follow:

# The only difference between assigning to a float and an integer is # whether or not the number has a decimal a: int = 2 b: float = 2.0

##### Operators

Character | Operator | Example | |
---|---|---|---|

+ | Addition | a = 12 + 14.3 # Returns 26.5 | |

- | Subtraction | a = 2 - 2.9 # Returns -0.9 | |

* | Multiplication | a = 4 * 1.5 # Returns 6 | |

/ | Division | a = 9 / 3 # Returns 3 | |

% | Modulus | a = 7 % 3 # Returns 1 |
Returns integer the integer remainder after division |

// | Floor division | a = 8 // 3 # Returns 2 |
Rounds down after division |