Python basic level course
Hello, World!
Python is a very simple language, and has a very straightforward syntax. It encourages programmers to program without boilerplate (prepared) code. The simplest directive in Python is the "print" directive - it simply prints out a line (and also includes a newline, unlike in C).There are two major Python versions, Python 2 and Python 3. Python 2 and 3 are quite different. This tutorial uses Python 2, because it is more widely used and supported. However, Python 3 is more semantically correct, and supports newer features.
For example, one difference between Python 2 and 3 is the print statement. In Python 2, the "print" statement is not a function, and therefore it is invoked without parentheses. However, in Python 3, it is a function, and must be invoked with parentheses.
To print a string, just write:
print "Hello World!"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."Basic Operators
This section explains how to use basic operators in Python.Arithmetic Operators
Just as any other programming languages, the addition, subtraction, multiplication, and division operators can be used with numbers.Try to predict what the answer will be. Does python follow order of operations?
number = 1 + 2 * 3 / 4.0
print numberAnother operator available is the modulo (%) operator, which returns the integer remainder of the division. dividend % divisor = remainder.
remainder = 11 % 3
print remainderUsing two multiplication symbols makes a power relationship.
squared = 7 ** 2
cubed = 2 ** 3
print squared
print cubedUsing Operators with Strings
Python supports concatenating strings using the addition operator:helloworld = "hello" + " " + "world"
print helloworld
lotsofhellos = "hello" * 10
print lotsofhellos
Comments
Post a Comment