USD ($)
$
United States Dollar
India Rupee

Learn Python from Scratch: Begin Here

Created by Ravish Rathi in Articles 6 Mar 2025
Share
«Why is Network Automation Important?

Welcome to Python Basics! This page is your gateway to mastering the fundamentals of Python programming.

Whether you're a beginner looking to start your coding journey or an experienced developer looking to brush up on the basics, this guide is designed to help you understand and work with Python in a clear and easy-to-follow manner. 

Python is a high-level, interpreted, and general-purpose programming language. It emphasizes code readability and simplicity, making it an excellent choice for both beginners and experienced developers.

This article covers the basics of Python, from setting up your environment to writing your first program and understanding key concepts like syntax, control flow, functions, and more. 


Python for Network EngineersLearn Automation with Python for Network Engineers.Explore course
custom banner static image

Creating Your Very First Python Program

Before we dive into the more complex features of Python, let's start by writing a simple "Hello World!" program. Python is known for its simplicity, and your first program will show you just how easy it is to get started. 


print("Hello World! Welcome to Python Basics") 

This program will output: 


Hello World! Welcome to Python Basics 

This is your very first Python program! It displays a message on the screen using the print() function. Now that you've written your first program, let’s learn how to add comments and more.

Python comments 

In Python, comments are used to explain the code and make it easier to understand. They are ignored by the interpreter and don't affect the execution of the program. 

Single-line Comments: 

Use the # symbol for single-line comments: 


# This is a Python comment 

Multi-line Comments: 

For multi-line comments, you can use triple quotes (''' or """): 


''' 

This is a  

multi-line comment 

''' 

Variables in Python 

A variable is a container that stores data. Python is dynamically typed, meaning you don't need to explicitly declare a variable’s type. 

Rules for Python Variables: 

1. A variable name must start with a letter (A-Z, a-z) or an underscore (_). 

2. A variable name cannot start with a number. 

3. A variable name can only contain alphanumeric characters and underscores. 

4. Variable names are case-sensitive. 

Example: 


# An integer assignment 

age = 45 

A floating point 

salary = 1456.8 

A string 

name = "John" 

Output


print(age)   

# 45 

print(salary)

# 1456.8 

print(name)   

# John 

Data Types in Python 

In Python, data types are used to define the type of a value. Python has several built-in data types, including: 

Integers: Whole numbers, like 5, 100, etc. 

Floats: Decimal numbers, like 3.14, 9.99. 

Strings: Sequences of characters, like "Hello". 

Lists: Ordered collections of items, like [1, 2, 3]. 

Tuples: Immutable ordered collections, like (1, 2, 3). 

Dictionaries: Collections of key-value pairs, like {"name": "John"}. 

Sets: Unordered collections of unique items, like {1, 2, 3}. 

Booleans: True or False. 

Complex Numbers: Numbers with a real and imaginary part, like 3+5j. 

Example: 


x = "Hello World"  # string 

x = 50             # integer 

x = 60.5           # float 

x = 3j              # complex 

x = ["geeks", "for", "geeks"]  # list 

x = ("geeks", "for", "geeks")  # tuple 

x = {"name": "Suraj", "age": 24}  # dictionary 

x = {"geeks", "for", "geeks"}  # set 

x = True            # boolean 

x = b"Geeks"        # binary

Input/Output Python 

The input() function allows you to accept user input. The input is always returned as a string, so you might need to convert it into another data type if required. 

Example: 


# Python program to show input and output 

val = input("Enter your value: ") 

print(val)  # Will display the entered value 

Python’s Operators 

Operators are used to perform operations on variables and values. Python supports several types of operators: 

Arithmetic Operators: Used for mathematical calculations like addition, subtraction, etc. 


a = 9 

b = 4 

print(a + b)   # Addition 

print(a - b)   # Subtraction 

print(a * b)   # Multiplication 

print(a / b)   # Division 

print(a % b)   # Modulus (remainder) 

print(a ** b)  # Exponentiation 

Logical Operators: Used to perform logical operations such as AND, OR, and NOT. 


a = True 

b = False 

print(a and b)  # Logical AND 

print(a or b)   # Logical OR 

print(not a)    # Logical NOT 

Bitwise Operators: Used for bit-level operations. 


a = 10 

b = 4 

print(a & b)    # AND 

print(a | b)    # OR 

print(~a)       # NOT 

print(a ^ b)    # XOR 

print(a >> 2)   # Right shift 

print(a << 2)   # Left shift 

Assignment Operators: Used to assign values to variables. 


a = 10 

b = a  

print(b)        # Output: 10 

b += a 

print(b)        # Output: 20 

b -= a 

print(b)        # Output: 10 

b *= a 

print(b)        # Output: 100 

b <<= a 

print(b)        # Output: 102400 

Python If Else 

The if statement is used for conditional execution of a block of code. The else statement lets you execute a block of code when the if condition is not met. 

Example 1: If-Else Statement 


i = 20 

if i < 15: 

    print("i is smaller than 15") 

else: 

    print("i is greater than 15") 

Output: 


I is greater than 15 

Example 2: If-Elif-Else Ladder 


i = 20 

if i == 10: 

    print("i is 10") 

elif i == 15: 

    print("i is 15") 

elif i == 20: 

    print("i is 20") 

else: 

    print("i is not present")

Python For & While Loops 

For Loop: 

The for loop is used to iterate over a sequence (like a list, tuple, or string). 


for i in range(0, 10, 2): 

    print(i) 

Output: 


While Loop: The while loop runs as long as the specified condition is True. 


count = 0 

while count < 3: 

    print("Hello Geek") 

    count += 1 

Output: 


Hello Geek 

Hello Geek 

Hello Geek 

Python Functions 

Functions are blocks of code that perform a specific task. Instead of writing the same code repeatedly, you can define a function and reuse it with different inputs. 

Example of a Simple Function: 


def evenOdd(x): 

    if x % 2 == 0: 

        print("even") 

    else: 

        print("odd") 

# Function calls 

evenOdd(2) 

evenOdd(3) 

Output: 


even 

odd 

Basics Of Python 

Python is an incredibly versatile and widely used programming language, favored for its simplicity, readability, and diverse applications.

Whether you're developing web applications, analyzing data, or building machine learning models, understanding the fundamentals of Python is essential for mastering this powerful language. 

In this section, we will explore the core building blocks of Python. These fundamentals will serve as the foundation for more advanced topics. We’ll cover a wide range of topics, from Python statements to operators, variables, and more. 

Understanding Python Statements 

A statement in Python represents a logical instruction that the interpreter can read and execute. Python statements are classified into two main types: 

Expression Statements: 

An expression is a statement that results in a value. These operations can be mathematical, logical, or even method calls. For example: 


(1 + 5) * 3   # Arithmetic expression 

pow(3, 2)      # Function expression 

Here, the arithmetic expression (1 + 5) * 3 evaluates to 18, and the function pow(3, 2) evaluates to 9. 

Assignment Statements: 

An assignment statement is used to assign a value to a variable. It’s a fundamental way to store and manipulate data. The basic syntax is: 

variable = expression 

Types of Assignment: 

Value-based Assignment: Python allocates a unique memory location when assigning a value. 


test1 = "Hello" 

print(id(test1))  # Outputs a memory address 

Current Variable Assignment: The new variable references the same memory location. 


current_var = "Python" 

new_var = current_var 

print(id(new_var))  # Same memory address as current_var 

Operation-based Assignment: You can also operate on the right-hand side before assignment. 


test = 7 * 2  # Assigns the result of the operation 

Multiline Statements: 

Python allows you to extend statements across multiple lines using two methods: 

Implicit Method: Using parentheses, brackets, or braces: 

a = (0 + 1 + 2 + 3 + 4 + 5) 

Explicit Method: Using the continuation character (\): 

a = 0 + 1 + 2 + 3 + 4 + 5 

Indentation: The Python Way 

Unlike many programming languages that use braces {} to define code blocks, Python uses indentation. This makes code more readable and visually structured. Python’s PEP 8 guidelines recommend an indentation of 4 spaces for each level of nesting. 

Example of Indentation: 


if True: 

    print("This line is indented") 

Indentation is mandatory, and failing to maintain proper indentation will lead to an IndentationError. 

Conclusion 

Mastering the fundamentals of Python will lay the groundwork for your journey into the vast world of programming.

From understanding core concepts like variables, operators, and statements to leveraging Python's powerful features like docstrings and operators, this knowledge will enable you to write efficient, readable, and scalable code. 

Now, it’s time to start practicing and experimenting with these fundamentals. The more you code, the better you’ll become. Happy coding! 

Operators, List and Tuple Methods in ...»
Ravish Rathi

Ravish Rathi is a currently working as a Senior Network Consultant with one of the world's largest Internet Service Provider. He started his career as network support engineer with HCL and since than he has been working on different roles with various organizations such as Accenture, IBM, HCL, HP etc. Now he is having more than 15 years of ...

More... | Author`s Bog | Book a Meeting

Related Articles

#Explore latest news and articles

Automation using Python for Network Engineers 7 Mar 2025

Automation using Python for Network Engineers

How Python skills and knowledge may help network engineers to grow in their career. Network Automation using Python is the most promising career at present.
Why is Network Automation Important? 12 Nov 2024

Why is Network Automation Important?

Discover the crucial role of network automation for network engineers. Learn why automation is essential for efficiency and competition in the networking field.
REST-Based APIs: Definition & Examples 10 Mar 2025

REST-Based APIs: Definition & Examples

REST-based APIs, their fundamental meaning, and practical examples. Gain insights into how these APIs function in modern web development.

FAQ

Yes, Python is beginner-friendly and can be learned from scratch using online tutorials, courses, and documentation. Its simple syntax makes it ideal for newcomers to programming.
Python is considered one of the easiest programming languages for beginners due to its readable syntax, vast community support, and extensive libraries that simplify coding tasks.
Python developer salaries vary by experience and location. Entry-level developers earn around $59,888-$118,400 annually, while senior developers can make up to $163,200 per year
Learning Python basics typically takes 100-150 hours, depending on prior programming knowledge. Mastery of advanced concepts may require several months of consistent practice.
Yes, Python is generally easier than Java due to its simpler syntax and dynamic typing. It requires less code for similar tasks and is more beginner-friendly.

Comments (0)

Share

Share this post with others

Contact learning advisor

Captcha image
SDWAN Live Training
SDWAN Live Training
Join Our SD-WAN Live Training | Expert-Led Training | Hands-On Labs | Certification Support!
Day
Hr
Min
Sec
Register Now