Introduction to Python: Starting From Scratch
What is Python?
Python is a dynamic language used for developing, scripting, testing, and programming. It is a general-purpose high-level language designed by Guido van Rossum in 1991 and developed by the Python Software Foundation.
It is easy to use, and its large collection of libraries and frameworks makes it one of the most reliable languages.
So, what are dynamic languages or dynamically typed languages, and how are they different from statically typed languages?
Dynamic languages are those where type checking happens while the program runs.
Hmm, then what is runtime? Runtime is when the program executes on the computer, meaning when the processor starts running the commands/code. Python has its own runtime environment called the Python Virtual Machine (PVM).
If you have learned languages like C++ or Java, you know that before running the code, we have to compile it first. Compilation is the process where your code converts into machine code, and type, syntax, and semantic checks are performed. However, in Python, we run the code directly without a separate compilation step. This is because Python automatically compiles the code itself.
Compilation in Python is simple: all the code from a .py file is converted into bytecode and saved as a cache file with a .pyc extension.
In languages like C or C++, you declare the datatype before the variable name, like int number = 10 or string greet = "Hi". But in Python, you don't need to declare datatypes. You can directly write number = 10 or greet = "Hi". This makes Python more flexible. One major advantage in Python is that the datatype of a variable can change dynamically. See the example below:
int variable; // variable number of integer type
variable = 10 ;//we can
variable = "Hello World" ;// Compile-time error: cannot assign a string to an integer variable
cout<<variable<<endl;
The following C++ code will give an error because the variable is defined as an integer at the start and can only store integers. But in Python
variable = 10
variable = "Hello"
print(variable) #this code will work completely fine and give output of Hello
This code will work fine because type checking in Python happens during runtime, and the code is interpreted line by line. On the first line, the variable is set to an integer, but on the second line, its type changes to a string. This is different from statically typed languages (C, C++, Java...) where you must declare the data type for each variable, which would lead to an error at compilation time.
variable = 10
print(type(variable))
variable = "Hello"
print(type(variable)) #this code will work completely fine and give output of Hello
#copy paste the code in IDE to see the type change of variable.
And do remember first the code gets compiled then it goes to runtime environment for the execution.
That's all for now.
Subscribe to my newsletter
Read articles from Harsh Panwar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by