About Python
- Python is a multi-paradigm programming language; Object Oriented and Procedural among others
Python Programming Environment Setup
First step is to set up the development environment.
You will need to download Python form here: http://www.python.org
What is a Variable?
A variable is a container where data is stored.
The data can be of any type.
Below you will find the definition of different data types.
Note: Unlike some other programming languages like Java in Python you do not have to declare the variable types.
Integer
A variable of type int is used to store integral values such as 1, 105 and 0.
Example
age = 12
length = 300
Float or Floating Point Number
A variable of type Float is used to store decimal numbers such as 1.4, 293.29 and -1322.76.
Example
temperature = 18.5
Boolean or bools
A Boolean data type can hold one of two values:
- True
- False
Example
result = "True"
String
A String data type holds a collection of characters known as a string.
Example
name = "Tom"
colour = "Green"
Printing Data Types with Python
The print() function is used for printing data types. More on functions later.
Example
print "Python Programming is fun!"
Alternatively , you may want to store the string value in a variable and then print its value. In both cases “Python is cool!” will be printed in the output console.
Example
myString = "Python Programming is fun!"
print myString;
Operators
Simple Assignment
Simply put, the assignment operator assigns a value to a variable.
Example
temperature = 35
After the above assignment, temperature variable holds a value of 35
Note that the value assigned can be changed later. In which case, the variable will hold the new value.
here we say:
temperature = 40
The variable now holds a value of 40.
Compound Assignment
a += b | is equivalent to | a = a + b |
a -= b | is equivalent to | a = a – b |
a *= b | is equivalent to | a = a * b |
a /= b | is equivalent to | a = a / b |
a **= b | is equivalent to | a = a **b |
a %= b | is equivalent to | a = a % b |
Arithmetic Operators
Arithmetic operators are used for mathematical calculations.
Addition (+), Subtraction (-), Multiplication (*) and Division (/)
((4 + 5 - 6) * 7 / 5)
Power (**)
3 ** 3
This is equivalent to 3 to the power of 3 and will return 27.
Modulus (%)
a % b will return the remainder of a/b
14 % 4
14/4 returns 3 with a remainder of 2. Therefore 14 % 4 returns 2.
Comparison Operators
The comparison operators returns a boolean.
In other words they return True or False.
Syntax
a == b a < b a > b