× back

Python

Python is a versatile and easy-to-learn programming language widely used for various applications, from web development to data analysis. It is known for its clean and simple syntax, which makes it beginner-friendly and powerful at the same time. With Python, you can work with different data types, perform mathematical operations, handle files, and even create complex applications. Whether you're writing a small script to automate a task or developing large-scale software, Python has tools and libraries that make coding easier and more efficient. In this section, we will dive into the basic concepts of Python, including its data types, variables, operators, functions, and control structures. We'll also explore how to handle inputs and comments, and how to use modules to extend Python's functionality.

Data types and variables

Variables and naming conventions

Operators and Operator Precedence

Operator Precedence Rules

Data Type Conversions

Python Data Structures

Command Line Arguments

Task 1: Accept name and age via command-line arguments and tell when the person will turn 100.

                
import sys

if len(sys.argv) != 3:
    print("All required Arguments are not there!! Usage: python3 test.py <name> <age>")
    sys.exit()

name = sys.argv[1]
age = int(sys.argv[2])
year_when_100 = 2025 + (100 - age)

print(f"Hello {name}, you will turn 100 years old in the year {year_when_100}")
                
            

Task 2: CLI Calculator

                
import sys

if len(sys.argv) != 4:
    print("Usage: python3 test.py <num1> <num2> <operation>")
    print("Operations: add, sub, mul, div")
    sys.exit()

num1 = float(sys.argv[1])
num2 = float(sys.argv[2])
op = sys.argv[3]

if op == "add":
    result = num1 + num2
elif op == "sub":
    result = num1 - num2 
elif op == "mul":
    result = num1 * num2 
elif op == "div":
    if num1 == 0:
        print("Error: Division by zero")
        sys.exit()
    result = num1 / num2 
else:
    print("Invalid Operation")
    sys.exit()

print(f"Result: {result}")
                
            

Data Input and Output

Comments

Import Modules

Control Statements

Python Built-in Functions

Python Modules

File Handling