Python Tutorial - Hello World
We will start with the basic Python Hello World
program.
>>> print("Hello World!")
Hello World!
That’s it! You can see the simplicity of Python by its syntax; no header files are included as in C\C++, no need to define any main function and so on. You also don’t need statement terminators, and you just write print and the content Hello World
enclosed in double quotation marks inside parenthesis.
Python Basic Syntax
Consider the code below which multiplies two numbers:
# Multiplying two numbers
x = 4
y = 2
result = x * y
print(result)
8
Explanation of Above Program
- The first line is a comment starting with
#
. Comments are the lines that are not executed but ignored. So they do not disturb the normal flow of a program. - The next line
x = 4
is the definition of a variable. The name of the variable isx
which stores 4. - Similarly, you can see
y
= 2.y
is a variable which stores 2. - The line
result = x * y
is a Python statement which evaluates the multiplication expressionx*y
and stores the result in the variableresult
. To multiple two number*
operator is used. print()
statement is used to print strings or values of variables on the screen.
You can note here there are no curly braces (delimiters); in Python to represent any block indentation is used. For example if you are creating a class, you need to use indentation as follows:
class1:
class2:
statements
class2:
statements
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn Facebook