DEV Community

Cover image for Python 101: Back to Basics Part 1
Abhay Singh Kathayat
Abhay Singh Kathayat

Posted on

Python 101: Back to Basics Part 1

Introduction

Python is one of the most popular programming languages in the world. Its simplicity, readability, and vast community support make it an excellent choice for beginners and professionals alike. Whether you're a complete novice or someone refreshing their skills, this guide will take you through the fundamental concepts of Python in an easy-to-understand manner.

Why Learn Python?

Before diving into coding, let's understand why Python is worth learning:

  • Simple Syntax: Python's syntax is clear and readable, making it easier for beginners to grasp.
  • Versatile: Python is used in web development, data science, machine learning, automation, and more.
  • Large Community: A massive global community ensures that help and resources are always available.
  • Extensive Libraries: Python has a rich ecosystem of libraries like NumPy, Pandas, TensorFlow, and Flask that make development faster and more efficient.

Setting Up Python

Step 1: Installing Python

To start coding in Python, install the latest version from the official website:
Python Official Website

After installation, verify the installation by running:

python --version
Enter fullscreen mode Exit fullscreen mode

(Use python3 --version on macOS and Linux.)

Step 2: Setting Up an Editor

You can write Python code in:

  • IDLE (Python’s built-in editor)
  • VS Code (Recommended)
  • PyCharm (Great for advanced users)
  • Jupyter Notebook (Preferred for data science)

Python Basics

1. Printing Output

The print() function is used to display text and data.

print("Hello, World!")
Enter fullscreen mode Exit fullscreen mode

2. Variables and Data Types

Python supports multiple data types, such as strings, integers, and floats.

name = "Alice"
age = 25
height = 5.6
is_student = True
Enter fullscreen mode Exit fullscreen mode

You can check the type of a variable using:

print(type(name))  # <class 'str'>
print(type(age))   # <class 'int'>
print(type(height)) # <class 'float'>
print(type(is_student)) # <class 'bool'>
Enter fullscreen mode Exit fullscreen mode

3. Taking User Input

To accept input from users, use the input() function.

name = input("Enter your name: ")
print("Hello, " + name + "!")
Enter fullscreen mode Exit fullscreen mode

4. Basic Operators

Python supports arithmetic and logical operators:

x = 10
y = 5
print(x + y)  # Addition
print(x - y)  # Subtraction
print(x * y)  # Multiplication
print(x / y)  # Division
print(x % y)  # Modulus
Enter fullscreen mode Exit fullscreen mode

5. Conditional Statements

Python allows decision-making using if, elif, and else.

num = int(input("Enter a number: "))
if num > 0:
    print("Positive number")
elif num < 0:
    print("Negative number")
else:
    print("Zero")
Enter fullscreen mode Exit fullscreen mode

6. Loops in Python

Loops are used for repetition.

For Loop

for i in range(1, 6):
    print(i)
Enter fullscreen mode Exit fullscreen mode

While Loop

count = 0
while count < 5:
    print(count)
    count += 1
Enter fullscreen mode Exit fullscreen mode

7. Functions in Python

Functions help organize and reuse code.

def greet(name):
    print("Hello, " + name + "!")

greet("Alice")
Enter fullscreen mode Exit fullscreen mode

Conclusion

This article covered Python's fundamental concepts, from variables to loops and functions. In the next part, we'll explore more advanced topics like data structures, file handling, and object-oriented programming. Stay tuned!

Happy Coding!

Top comments (0)