Python Basics

Learn python basics|Python Indentation|python comments|python variables|codigence
python programming language | codigence

Python Basics Codigence

Hello World Program 😃


print("Hello World")

Output


Hello World

Python Indentation

What is Indentation?

  • Indentation refers to the spaces at the beginning of a code line.
  • Python provides no braces to indicate blocks of code for class and function definitions or flow control.
  • Python uses indentation to indicate a block of code.

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

Example :


if 5 > 4:
  print("Five is greater than four!")

Syntax Error:


if 5 > 4:
print("Five is greater than four!")

📝 Note: The number of spaces is up to you as a programmer, but it has to be at least one.

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

Example :


#first block
if 5 > 4:
  print("Five is greater than four!")
  print("Five is greater than two!")
#second block
if 5 > 4:
    print("Five is greater than four!")
    print("Five is greater than two!")

Syntax Error:


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

Python Comments

  • Comments can be used to explain Python code.
  • Comments can be used to make the code more readable.
  • Comments can be used to prevent execution when testing code.

Single Line Comment :


#This is a comment
print("Hello, World!") #print('hello world')

Multi Line Comment


'''this is multi line comment
written in more than one line '''
print("Hello world!")

Python Variables

  • Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.
  • Python has no command for declaring a variable.
  • In Python, variables are created when you assign a value to it:

Example :


x = 5 #variable 'x' having the value 5
y = "Hello, World!"
print(x)
print(y)