DEV Community

V Sai Harsha
V Sai Harsha

Posted on

Learn Python Basics under 50 lines

Python is a popular programming language known for its simplicity and readability. Whether you're a beginner or an experienced programmer, learning Python can be a valuable skill to have. In this article, we will cover the basics of Python and guide you through the process of learning the language in just 100 lines of code. Let's get started!

  1. Hello, World!: Every programming language tutorial starts with a simple "Hello, World!" program. In Python, it can be accomplished in just one line:
print("Hello, World!")
Enter fullscreen mode Exit fullscreen mode
  1. Variables and Data Types: Python is dynamically typed, meaning you don't need to declare variables explicitly. Here's an example of a variable assignment:
name = "John"
age = 25
Enter fullscreen mode Exit fullscreen mode
  1. Lists: Lists are a versatile data structure in Python. You can create a list and perform basic operations on it:
fruits = ["apple", "banana", "orange"]
fruits.append("grape")
Enter fullscreen mode Exit fullscreen mode
  1. Control Flow: Python provides conditional statements and loops for control flow. Here's an example of an if-else statement and a for loop:
if age >= 18:
    print("You are an adult.")
else:
    print("You are a minor.")

for fruit in fruits:
    print(fruit)
Enter fullscreen mode Exit fullscreen mode
  1. Functions: Functions allow you to encapsulate reusable code. Here's how to define and call a function:
def greet(name):
    print(f"Hello, {name}!")

greet("Alice")
Enter fullscreen mode Exit fullscreen mode
  1. Dictionaries: Dictionaries are key-value pairs that allow you to store and retrieve data efficiently:
person = {"name": "Alice", "age": 30}
print(person["name"])
Enter fullscreen mode Exit fullscreen mode
  1. File Operations: Python makes it easy to read from and write to files. Here's an example of reading from a file:
with open("file.txt", "r") as file:
    content = file.read()
Enter fullscreen mode Exit fullscreen mode
  1. Exception Handling: Python provides a way to handle errors gracefully using try-except blocks:
try:
    result = 10 / 0
except ZeroDivisionError:
    print("Cannot divide by zero.")
Enter fullscreen mode Exit fullscreen mode
  1. Classes and Objects: Python supports object-oriented programming. Here's an example of creating a class and an object:
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height

rect = Rectangle(4, 5)
print(rect.area())
Enter fullscreen mode Exit fullscreen mode
  1. Libraries: Python has a vast ecosystem of libraries that extend its capabilities. Here's an example of using the random library:
import random

random_number = random.randint(1, 10)
print(random_number)
Enter fullscreen mode Exit fullscreen mode

Conclusion

These 10 code snippets cover the basics of Python programming. By understanding and practising them, you'll gain a solid foundation in Python development. Remember, learning programming is not just about writing code, but also about solving problems and building real-world applications. So, keep practising, exploring, and building upon what you've learned. Happy coding!

Top comments (0)