Python is a powerful and versatile programming language widely used in various fields such as web development, data science, artificial intelligence, and more. It is known for its simple and easy-to-read syntax, making it an excellent choice for beginners to learn to program.
Let’s understand the basics of a python program.
A Python program is a collection of instructions that tell a computer to perform a specific task. A program can be as simple as displaying a message to the user or as complex as creating a machine-learning model.
A basic Python program consists of the following elements:
Comments are lines of text in a program that the Python interpreter ignores. To add an explanation to our code we can use comments. Comments are denoted by a
# symbol and can be placed anywhere in the code.
# This is a comment in python
To store and manipulate data in a program we can use variables. They have a name and a value, and the value can be changed during the execution of the program. In Python, you can use any name you want for a variable, as long as it starts with a letter or an underscore and contains only letters, numbers, and underscores
message = "hello world" age= 24
Here the name of the variable is the “message” and its value is “hello world”, the second example age is a variable of datatype integer with the value of 24.
3. Data Types
Python supports several data types such as integers, floating-point numbers, strings, and more. Data type serves two purposes:
- We can determine the kind of value a variable can store
- We can identify the operations we can perform on a variable
. For example, you can perform mathematical operations on integers but not on strings.
Operators are special symbols in Python that perform operations on variables and values. The most common operators are mathematical operators such as
/. Python also supports comparison operators such as
!= which are used to compare values.
5. Control Flow Statements
To control the flow of execution of a program we can use control flow statements. We use these statements to make decisions in the program and repeat certain actions.
They include :
- if else statements (decision)
- for loop (repeat)
- while loop (repeat)
- break statement (decision)
- continue statement (decision)
Functions are reusable blocks of code that perform a specific task. They can take input parameters and return an output. Functions make the code more organized and easy to understand. Python has several built-in functions such as
len() and you can also create your own functions using the
def greeting(): print("Welcome to Python programming")
Modules in Python are pre-written code that can be imported and used in a Python program. They are used to extend the functionality of Python by providing additional functionality and libraries that are not included in the core Python language.
Eg: os module, sys module
Python program example
Let’s see a basic python program example.
In Python, the “main” function is the entry point of a program. The program calls this main function and it usually performs the main tasks of the program. The main function is typically defined using the
if __name__ == "__main__": statement, which allows the code in the function to be executed only when the script is run directly and not when it is imported as a module into another script. The main function can be called
main() or any other name.
Here is an example of a simple Python program that defines a
def main(): print("Hello, World!") if __name__ == "__main__": main()
You can copy this code and name the file “helloworld.py”. To run the code simply type
In this example, the
main() the function is defined at the top of the script. It simply prints the string “Hello, World!” to the console.
In conclusion, Python programming is easy to learn and it is very powerful.