DEV Community

Cover image for Python Syntax
Introschool
Introschool

Posted on

Python Syntax

Write your first Python Program

The print function

print("Hello World!")

# Output
# Hello World!

Enter fullscreen mode Exit fullscreen mode

Math operations on two numbers
If you have two numbers and you want to perform math operations on those numbers then it is very simple.

# Addition
5 + 5

# Subtraction
10 - 5

# Multiplication
3 * 2

# Division
20 / 4

# Output
# 10
# 5
# 6
# 5.0

Enter fullscreen mode Exit fullscreen mode

Basic Concepts of Python Programming

In this section, we will discuss the basic concepts that you need to know before starts programming in Python.

Keywords and Identifiers

Every programming language has its syntax. It means that there is a particular way of writing instructions in each programming language. While writing a program or instructions, you deal with different types of data. It could be a string, number, or native Python data types.

There are variables and functions in Python. Later we will discuss them in detail, but for now, assume them as an entity. So to identify each entity, we can give them a name and use that name anywhere in the program. That name will refer to a particular entity.

See the below example

# Here a and b are variables.
a = 2
b = 5

print(a + b)

# Output
# 7

Enter fullscreen mode Exit fullscreen mode

What are Identifiers?
Identifiers are the names given to the variables, functions, classes so that in the program you can identify that particular variable, function or class with that particular name.

What are Keywords?
Just like human languages, computer languages also have grammar. We have to write our program according to the grammar of the programming language. In the grammar of computer languages, Keywords are special names that are reserved. It means that you can not use keywords to give a name to variables and functions. We will discuss these Keywords later in the course.

List of Keywords in Python

False             def             if              raise
None              del             import          return
True              elif            in              try
and               else            is              while
as                except          lambda          with
assert            finally         nonlocal        yield
break             for             not                 
class             from            or                  
continue          global          pass  

Enter fullscreen mode Exit fullscreen mode

Values, Statements and Expressions

Values
In terms of computer science, a value could be any entity that can be manipulated by computer programs. A Value could be number, string, a special character like $, @, #, etc…
Values are stored in variables.

Statement
The Statement is any instruction that Python interpreter can execute. You have seen an assignment(=) statement and a print statement. So whenever the interpreter sees the assignment statement it assigns the value to the particular variable and when it sees print statement it prints the value. There are other statements like if, else, for, etc.., We will discuss them later in the course.

Expression
The expression is a combination of values, operators, and variables. If you write an expression, the Python interpreter first evaluates it and then gives the output.

a = 2
b = 8

sum = a + b

print(sum)

# Here 2, 3 are values.
# Print(sum) is a statement
# sum = a + b is an expression.
# The interpreter will first evaluate a + b and then it will give the value


Enter fullscreen mode Exit fullscreen mode

Comments

Comments are a really important part of the programming. Comments are the lines that are ignored by the Python interpreter. By writing comments you can make your code more readable. There is this one quote by Martin Fowler -
“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” — Martin Fowler
You don’t write comments for the computer, you write comments so that other people can understand it.
How to Write Comments in Python
To write comments in Python, just put # before your comments.

print("This is a  comment.")  # This is a comment.

# Output 
# This is a comment.

Enter fullscreen mode Exit fullscreen mode

The Python interpreter will ignore the comment and execute the print statement.
If you want to write multi line comments, there are two ways-

  1. You can use multiple # to write multi line comments.
  2. You can wrap your comment inside a set of triple quotes.
# You can write multi line
# comments in this way.

'''
Or if you don't like using hash(#)
You can use triple quotes also 
To write multi line comments
'''
Enter fullscreen mode Exit fullscreen mode

Indentations
Indentations are used to denote the block of code.

What is block of code?
Later we will use function statements like If, else, for, while. A block of code is a body of a function or statement(if, else, for, while). So to denote a code block Python uses indentation.

Python uses whitespace to indent the code. Whitespace is any character that represents the vertical or horizontal space. To indent the code we use spacebar or tab key.

You can give any number of spaces in the indentation but the number should be fixed throughout the block. Generally, four spaces are used for indentation.

In other languages, to denote the block of code curly braces are used but in Python, indentations are used and it is strictly enforced.

We will understand indentation with examples. See the below code.

def check_password(password):
    if password == "Helloworld"
        print("password is correct!")
    else:
        print("Wrong password")

Enter fullscreen mode Exit fullscreen mode

In the above example, there is a function called check_password. It checks if the given password is correct or not. If the given password is correct it prints “password is correct”, otherwise it prints “Wrong password”.

In the above example, we used four spaces for indentation. You can easily see the body of a function, if statement and else statement.

Indentations make your code look pretty and clear.

Top comments (0)