Thursday, May 16, 2013

Python tutorial Part 1 variables int, float, str, and simple math

Variables refer to memory addresses that store values.  This means all variables are stored in memory.  Therefore for big data applications you will have to keep this in mind.  You can find out the maximum size of lists, strings, and dicts on your system by the code below:




How to Assign integers to variables and subtraction:

The equal sign (=) is used to assign variables in python.  The negative sign (-) is used for subtraction and type() is used to get the type of the variable.  The type of is int in the case below. For example:







How to Assign floating point numbers to variables and addition:

The positive sign (+) is used for addition







Examples of adding and subtracting float and int:











Division of int:

Floating point division uses (/) sign.  This type of division divides left hand operand by right hand operand and returns a floating point number.   Floor Division uses (//) sign.  This type of division is returns only the whole int part of division without any rounding. The modulus (%) operand returns the remainder. 











Division of float:

Division of float returns only type float and everything else is the same as above.





 



Multiply int and float:

The multiply operand is (*).   If the type is only int it returns int.  If either type is float it returns a float.









Exponent of int and float:

The exponent operand is ( **).  If the type is only int it returns int.  If either type is float it returns a float.












How to assign strings and concatenate strings:

To assign variables to strings inclose strings with (' ') or (" ").  As you can see below strings can be concatenated by the plus sign (+) and if you need a space between the words it needs to be added in.  The type of strings is str.










Variable name guide lines:

For python style use lower_case_with_underscores for variable names.  The words are separated by underscores to improve readability.





Variable names can not start with a number