Skip to content

Python Basic Syntax




Python – Basic Syntax


Python programming language is considered to be a very good language for a beginner and a beginner should know these basic concepts of Python such as basic syntax in python, basic python commands, basic python script, python Indentation, python Comments, Joining two lines etc. and whatever you are studying, try to execute it in your computer or laptop as well, and this will increase your coding skills very quickly.

 

Add python 3.8 to PATH windows 10 / Linux or Unix

 



How to execute Python Syntax ?

Python Syntax can be executed by writing directly in the PyCharm IDE :



Example :

print("Hello, CodifyShow !!")



Output :

Hello, CodifyShow !!

 

If you creating a Python file on the server, using the .py file extension, and running it in the command line :

C : / users / your name / python file.py




Python Indentation

Python uses whitespaces (spaces and tabs) to define program blocks whereas other languages like C, C++ use braces ( { } ) to indicate blocks of codes for Class, Functions or Flow Control. The number of whitespaces including spaces and tabs in the indentation is not fixed, but all the statements within the block must be the indented same amount. In the following python program, the block statements have no indentation.



Example :

Python-Basic-Syntax



Output :

ten is greater than five




Python will definitely give you an error if you skip the Indentation.



Example :

if 10 > 5:
print("ten is greater than five")



Output :

IndentationError: expected an indented block




As a programmer, The number of spaces is up to you but it has to be at least one.



Example :

if 10 > 5:
 print("ten is greater than five")

if 10 > 5:
 print("ten is greater than five")



Output :

ten is greater than five
ten is greater than five




You have to use the same number of spaces in the same block of code, otherwise Python will definitely give you an error :



Example :

if 10 > 5:
  print("ten is greater than five")
             print("ten is greater than five")



Output :

IndentationError: unexpected indent




What are comments in python ?

Python comments start with a hash mark ( # ) and whitespace character, and python will render the rest of the line as a comment :



Example : Comments in Python

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



Output :

Hello, CodifyShow !




Joining Two Lines

When you want to write a long code in a single line then you can break the logical line in two or more physical lines using backslash ( \ )character.

Therefore when a physical line ends with a backslash characters ( \ ) and not a part of a string literal or comment then you can join another physical line. see the following example :



Example :

u = 0
v = 1
w = 2
x = 3
y = 4
z = 5

if u == 0 and v > 0 \
and w > 1 and x > 2 \
and y > 3 and z > 4:
    print("Example of line joining")



Output :

Example of line joining



Multiple Statements on a single line

We can write two separate statements into a single line using a semicolon ( ; ) characters between two lines.



Example :

print("Hello, Codify")
print("Hello, Show")

# You can also write above two statements in the following way

print("Hello, Codify") ; print("Hello, Show")



Output :

Hello, Codify
Hello, Show
Hello, Codify
Hello, Show




Python Coding Style

  • You have to use 4 spaces per indentation and no Tabs.

  • Do not mix tabs and spaces because tabs create confusion and it is recommended to use spaces only.

  • Maximum line length is 79 characters which help users with a small display.

  • Use inline comments ( should be complete sentence ).

  • Use blank lines to separate function and class definitions and single blank line to separate methods definitions inside a class and larger blocks of code inside functions.

  • Use spaces around expressions and statements.