DEV Community

suraj kumar
suraj kumar

Posted on

Python for Beginners: Write Your First Python Program

Introduction to Python:
Python is a high-level language created by Guido van Rossum in 1991. A high-level language means that he code is written in a human-readable language, which makes it easier for humans to understand and write the code in a human-readable form. The code is then converted into machine code by the compiler.
The machine code is the binary language that consists of a sequence of 0s and 1s. Computer’s hardware, or so-called CPU(Central Processing Unit), only understands the binary language or low-level language. Python’s easy syntax allows the programmer to express their program in fewer lines of code. The interpreter then executes the code line by line and gives the output of the program.
Applications of Python Programming
The Following are the applications of the Python Programming language

  1. Machine Learning: Python is widely use in developing various Machine Learning models. The Deep Learning and Neural Networking program is written in the Python Programming Language. It provides the support of different libraries like NumPy, TensorFlow, Scikit-learn, Pandas, etc, which offer other tools and functionality for data manipulation, analyzing the given data, and help build machine learning models. Different algorithms used for machine learning, like Linear Regression, Supervised Learning, Unsupervised Learning, K-Nearest Neighbours(KNN), are written in Python .

  2. Web Applications: Python offers the support of different frameworks like Django, Flask, Pyramid, etc., which are widely used for building secure and advanced web applications. These frameworks provide various features like a structured approach while writing the program, database integrity, server handling or URL Routing, and ensuring high security to web applications.

  3. Game Development: Python provides support for Pygame, which is a very popular gaming library that supports 2D graphics, sound, and user input, simplifying the gaming development process. It also provides other libraries as well, which are very helpful in game development, like Panda3D. This library is an open-source engine supporting 3D graphics for game development and strong animation support for providing animation to the models or characters in the game and to the game environment.

  4. Cybersecurity: Cybersecurity is the practice of protecting your computer systems, various accounts, networks, and data from external theft or damage. There are various unauthorised activities that happen on digital platforms, like phishing, malware, hacking, ransomware, SQL injection, etc. All aim at capturing or stealing your data, which may cause interruptions in your services or may cause financial harm.

Protecting your data and keeping it secure is essential in today’s world because most things are digital nowadays and happen through a mobile or laptop. Python helps a lot in protecting your data and keeping them secure because of the extensive libraries it provides example Scapy, Nmap, Requests, etc.

  1. Desktop Applications: Desktop applications are software programs that can run on an operating system without requiring the use of a web browser for example VLC media player, MS Word, MS Excel, Adobe Photoshop and others. These kind of applications can be built with the help of Python. It is a very popular choice among developers for developing such types of applications because of various reasons, like

• Ease of Use: The easy and readable syntax of Python makes it very much easy for any human being to learn Python.
• Rich Source of Libraries: Python provides various libraries which helps a lot in building web applications, games, data science and in other fied as well.
• Integration Capabilities: Python can easily integrate with platforms like can easily integrate with databases, APIs, and other programming languages, which makes it a highly productivity tool for developing applications and suitable for performing complex task.

Data Types in Python
There are six different kinds of data types in Python. They are
• Numeric data type
• Sequence data type
• Mapping type
• Boolean type
• Set type
• Binary type
Given below is the description of all different kinds of data types
1) Numeric data type
These data types specify the integer kind of values, which include int, float, and complex. The int data type categorizes the number that can be either positive or negative, but does not include the fraction part. The example of an int data type can be -7 or 23, but not 7.23.
The float data type use for fractional values. Example of float data type is 3.14, -7.8 or 12.56. Complex data type include both real and imaginary values. Example of complex values are 7 – 5j where 7 is a real number and 5 is an imaginary number.
2) Sequence data type
The sequence data type in Python represents a group of similar or different data types in python. It can either be a list, string, or tuple. In Python, there is no specific data type for declaring a string. String can be declared with a single character containing the array of characters enclosed within the quotes. For example, s = "It is raining outside". Lists refer to a group of characters; the characters might be of the same data type or a different data type.
Example:
w = [“Noida” , “Delhi”, 5 , 8]

The difference between List and Tuple is that data can not be modified in Tuple once it is created whereas in List data can be modified.
3) Mapping type
Mapping refers to the data structure in which the values are stored in key-value pairs. Value can be of any data type and must be mapped to a key which is unique and should not be repeated.
Example: Given below is an example mapping data type
u = {1 : ‘Programming’ , 2 : ‘in’ , 3 : ‘Python’}
print(u)

Output:
{1 : ‘Programming’ , 2 : ‘in’ , 3 : ‘Python’}
4) Boolean type
Boolean data type only holds two values at a time, either true or false. It is commonly used in programming where logical work is required or to control the flow of a program through a conditional statement. Boolean data type is used for decision making in programming. It executed the different blocks of code based on a conditional statement, which can either be true or false.
Example: Python Program on boolean data type executing conditional statement
a = 60
b = 183
if a > b:
print("a is greater than b")
else:
print("b is greater than a")
Output:
b is greater than a

5) Set type
Set is another data type used to store multiple items of different data types in a single variable. The other data types are List, Tuple, and Dictionary. All these data types are used according to their usage and qualities in a program. Set items are unordered, unchangeable, and do not allow duplicate values.

Set items cannot be change, but you can remove and add new items in Set data type. Sets are declared in Python by placing all the elements inside the curly braces {}, and commas separate each element. A set contains the list of items, and each item can be of different data types (example: integer, float, string, boolean , etc.).

Example:
theset_id = {“Python” , 78 , False, “apple”, 9}
Output:
{False, “Python”, 9, “apple”, 78}
6) Binary type
Binary data type represents the comprising sequence of 0's and 1's. Binary data type is further divided into three categories, which are bytes, bytearray, and memoryview. In programming, binary data type allows for representing the data in its most basic form, which are sequence of 0's and 1's, allowing the computer to process and store the data efficiently.

Bytes provides a sequence of 0's and 1's which can not be change, whereas bytearray allows modification in the sequence of 0's and 1's, meaning the content or data store in bytearray can be change or modified.

Basic Programs in Python

  1. Python Program to add two numbers
    a = 7
    b = 8

    Addition of two numbers

    result = a + b
    print(result)
    Output:
    15

  2. Program in Python to find the area of a circle
    import math
    r = 89 # radius of a circle
    area = math.pi*math.pow(r , 2)
    print(area)

Output:
24884.55540908475

  1. Program in Python to find the Simple Interest

def fun(p, r, t):
return (p * r * t) / 100;

assigning the values of principal, rate, and time

p, r, t = 100, 5, 2
interest = fun(p, r, t)
print(interest)

24884.552488
Output:
10.0
4.5554090847524884.55540908
Conclusion

Writing your first Python program marks an essential milestone in your programming journey. Python’s clean and readable syntax, combined with its powerful features, makes it an ideal choice for beginners. As you continue practicing and experimenting with code, you'll gain confidence and unlock the full potential of Python as a versatile and beginner-friendly programming language. As you continue practicing and experimenting with code, you'll gain confidence and unlock the full potential of Python as a versatile and beginner-friendly programming language.

I suggest you learn the concept of Python Programming from the Tpoint tech website, as this website covers various topics in Python and clears your understanding of Programming as well. It provides interview questions and an Online Python Compiler too.

Top comments (0)