Pyhon Syntax,Variables and the fundamentals

We can use the print statement to output a result:

Python

print("Hello World")

Output

Hello World

The Python interpreter can be used as a calculator:

Python

print(5+5/2)

Output

7.5

How to use Variables in Python

Variables are containers for storing data values.

We can store data into variables for example:

Python

weight = 60

To do anything useful with data, we need to assign its value to a variable. In Python, we can assign a value to a variable, using the equals sign ‘=’. For example, to assign value ’60’ to a variable ‘weight’, we would execute:

From now on, whenever we use weight, Python will substitute the value we assigned to it. In layman’s terms, a variable is a name for a value.

In Python, variable names:

  • can include letters, digits, and underscore
  • cannot start with a digits
  • are case sensitive

To display the value of a variable to the screen in Python, we can use the print function:

Python

print(weight)

Output

60

Moreover, we can do arithmetic with variables right inside the print function:

Python

print(2.2 * weight)

Output

132.0

How to use Comments in Python

Comments start with a #, and Python will render the rest of the line as a comment:

Python

#This is a comment.
print("Hello, World!")

Comments can remind you what functions do or allow you to stop lines of code from running