Introdcution To Python
Python is a very simple language, and has a very straightforward syntax. It encourages programmers to program without boilerplate (prepared) code.
There are two major Python versions, Python 2 and Python 3. Python 2 and 3 are quite different. This tutorial uses Python 3, because it more semantically correct and supports newer features.
print("This line will be printed.")
Indentation
Python uses indentation for blocks, instead of curly braces. Both tabs and spaces are supported, but the standard indentation requires standard Python code to use four spaces. For example:
x = 1
if x == 1:
# indented four spaces
print("x is 1.")
Output : x is 1.
Variables and Types
Python is completely object oriented, and not "statically typed". You do not need to declare variables before using them, or declare their type. Every variable in Python is an object.
This tutorial will go over a few basic types of variables.
Numbers
Python supports two types of numbers - integers(whole numbers) and floating point numbers(decimals). (It also supports complex numbers, which will not be explained in this tutorial).
To define an integer, use the following syntax:
myint = 7
print(myint)
Output : 7
To define a floating point number, you may use one of the following notations:
myfloat = 7.0
print(myfloat)
Output : 7.0
Strings
Strings are defined either with a single quote or a double quotes.
mystring = 'hello'
print(mystring)
Output : hello
The difference between the two is that using double quotes makes it easy to include apostrophes (whereas these would terminate the string if using single quotes).
mystring = "Don't worry about apostrophes"
print(mystring)
Output : Don't worry about apostrophes
There are additional variations on defining strings that make it easier to include things such as carriage returns, backslashes and Unicode characters. These are beyond the scope of this tutorial, but are covered in the Python documentation.
Simple operators can be executed on numbers and strings:
one = 1
two = 2
three = one + two
print(three)
hello = "hello"
world = "world"
helloworld = hello + " " + world
print(helloworld)
Output : 3
hello world