Essentials of Python – The Basics of Python 3.9

Every programming language has its own style like syntax and set of rules. If we talk about Python, it has its own essentials. For example, we know that python uses indentation for defining a block of code. So, it is very important to take care of indentation while writing python codes as well as its structure so that the interpreter can interpret and run it.

Essentials of Python

Basic Input and Output

The most basic functions of any programming language are its input and output functions like the most famous first program of any language is the "Hello World" program. So in python, we simply use a print() function for output. And input() function for input.

Code For print() Method

print("Hello World!") 

Code For input() Method

name= input("Enter your name:  ")

This input can be stored in any variable like name = input(“Enter your name: “), and the input will be saved in the variable name.

Flow-control

Flow-control can be defined as the order of execution of instructions, written in the program. In python, instructions are executed line by line. It will execute the first line, then the next line (written under the first), and so on in the order in which the statements are written. The flow of the program can be controlled by flow control statements which include loops and conditional statements.

Indentation and Scope

In languages like C++ or Java, we use curly brackets { } for defining the scopes. But in python, the scope of a block is defined by indentation. 

Consider the following example:

# starting of a new block 
def myFunction(num):
      # indented lines are inside the block of function myFunction
      # a new block inside the block myFunction
      if (num >= 0): 
           print("Number is positive.") 	
           # this line is in if block
      # a new block inside the block myFunction
      else:	     
           print("Number is negative.")
           # this line is else block
      # this is inside the block (indented)
  # this is out of the block (not indented)
  print(“Number is checked!”)

** Hash (#) shows comment in Python

Data Types

In python, there is no need of writing data types with variables or identifiers for variable declaration.

Variables

A variable is declared when a value is assigned to it. The data type of this variable depends upon the type of value which is assigned to it. If we write x = 5 and check the data type of the variable x by using the type() method like print(type(x)), it will show the data type of x that is int in this case.

Similarly, for some other cases, the data type of x will be;

x = 5.5                         #(float)
x = 5j							#(complex)
x = True						#(boolean)
x = ‘AlgoIdeas’ or x = "AlgoIdeas"			#(string) 
x = ["Algo", "Ideas"] 					#(list)
x = ("Algo", "Ideas")					#(tuple)
x = {"Firstname" : "Algo", "Lastname" : "Ideas"} 	#(dictionary) 
x = {"Algo", "Ideas"}					#(set)

Type Conversions

To convert one type to another we simply use the data type in which we want to convert with the variable name. Like we want to convert an int to a floating value, consider the following example:

num = 5
print (type(num))
numf = float(num)
print (type(numf))

Output

int
float

Stay in the Loop

Get the daily email from Algoideas that makes reading the news actually enjoyable. Join our mailing list to stay in the loop to stay informed, for free.

Latest stories

- Advertisement -

You might also like...