DEV Community

Sona
Sona

Posted on

Python Syntax Comments & Variables - Learn Here

Python syntax can be executed by writing directly in the Command Line:

print("Hello, World!")
Hello, World!


Or by creating a python file on the server, using the .py file extension, and running it in the Command Line:

C:\Users\Your Name>python myfile.py

Python Syntax Comments & Variables

Learn Everything About Python Syntax Comments & Variables. Python for beginners, python syntax, python variables, variables in python.

favicon codemagnet.in

Python Indentation:

Indentation pertains to the spaces located at the start of a code line.

While in some programming languages, indentation primarily serves readability purposes, in Python, it holds significant importance.

In Python, indentation is utilized to signify a block of code.

Lets take an example:

if 5 > 2:
print("Five is greater than two!")

Python will give you an error if you skip the indentation:

Syntax error below code:

if 5 > 2:
print("Five is greater than two!")

Note: The number of spaces is up to you as a programmer, the most common use is four, but it has to be at least one.

Right Way to write code:

if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")

Wrong Way which will give you syntax error:

if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")

Python Comments:

Python features a commenting capability designed for in-code documentation.

Comments in Python commence with a # symbol, and the remainder of the line is interpreted by Python as a comment.

comment in python:

This is a comment.

print("Hello, World!")

Python Variables:

Variables serve as containers for storing data values.

How to create a variable in Python?

Like other programming language, Python has no command for declaring a variable.

A variable is created the moment you first assign a value to it.

x = 5
y = "John"
print(x)
print(y)

There is no requirement to declare variables with a specific type in Python, and their type can be altered even after being initially assigned.

x = 4 # x is of type int
x = "Sam" # x is now of type str
print(x)

If you want to specify the data type of a variable, this can be done with casting. Look below how casting is done

x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0

Don't hesitate to copy codes and test it out yourself. Remember when you will practice yourself then only you will learn.

Top comments (0)