Python is a high-level, interpreted programming language known for its simplicity and readability. Developers widely use it in various fields such as web development, data science, automation, and more. In this guide you will explore Python Programming Basics for beginners.
Python syntax is clean and easy to understand.
Python is case-sensitive language. Thus, there’s no need to insert terminator at end of line.
Additionally, some basic data types include:
x = 5
y = 3.14
name = "Alice"
is_valid = True
Now that you are familiar with Python’s basic data types, let’s look at how to store and manipulate data using variables and operators.
Variables store data that you can use and manipulate.
Python is non-typed language, due to which there is no need to predefine data types of variable.
a = "hello world"
b = 10
c = 10.5
Operators perform operations on variables:
+
, -
, *
, /
==
, !=
, >
, <
and
, or
, not
Control flow statements determine the execution flow of a program.
if condition:
# code block
elif another_condition:
# another code block
else:
# code block
Loops execute code repetitively.
You can use For loop when you know number of iterations.
for i in range(5):
print(i)
While loop is used to repeat a specific block of statements until a condition is met
while condition:
# code block
Functions are reusable blocks of code. We can define functions using the def keyword.
Essentially, In-built functions are pre-defined in python library, and you can directly use them without declaring them.
print("Hello world!")
Users define the functions on their own, and you can also declare these functions using the def keyword.
def function_name(parameters):
# code block
return value
function_name(attributes)
Lists , set , tuple and dictionaries are built-in data structures in Python.
You can read from and write to files in Python using in-built functions.
with open('filename.txt', 'r') as file:
content = file.read()
You can handle errors in Python using try, except, and finally blocks.
try:
# code block
except Exception as e:
# handle exception
finally:
# code block (always executed)
Python supports Object-Oriented Programming (OOP), which allows you to create classes and objects, making your code more modular, reusable, and easier to maintain.
A class is a blueprint for creating objects (instances). Each object can have attributes (variables) and methods (functions) defined in the class.
class MyClass:
def __init__(self, name):
self.name = name # Attribute
def greet(self):
print(f"Hello, {self.name}") # Method
# Creating an object
obj = MyClass("Alice")
obj.greet() # Output: Hello, Alice
class MyClass:
def __init__(self, name):
self.name = name
print(f"{self.name} created")
def __del__(self):
print(f"{self.name} destroyed")
obj = MyClass("Alice")
del obj # Output: Alice destroyed
Inheritance allows a new class to inherit attributes and methods from an existing class. The new class is called the derived or child class, and the existing class is the base or parent class.
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
dog = Dog()
dog.speak() # Output: Animal speaks
dog.bark() # Output: Dog barks
Encapsulation is the bundling of data and methods that operate on that data within a single unit or class. It also involves restricting access to some of the object’s components, which is achieved through private or protected attributes.
class MyClass:
def __init__(self, name):
self.__name = name # Private attribute
def get_name(self):
return self.__name
obj = MyClass("Alice")
print(obj.get_name()) # Output: Alice
Polymorphism allows different classes to be treated as instances of the same class through inheritance. It enables methods to be used interchangeably between objects of different classes.
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def speak(self):
print("Dog barks")
class Cat(Animal):
def speak(self):
print("Cat meows")
def make_animal_speak(animal):
animal.speak()
dog = Dog()
cat = Cat()
make_animal_speak(dog) # Output: Dog barks
make_animal_speak(cat) # Output: Cat meows
Modules are Python files containing definitions and statements. Packages are directories containing multiple modules.
Python has a vast ecosystem of external libraries that extend its functionality.
import numpy as np
import pandas as pd
You’ve built a solid foundation in Python Basic Programming, covering everything from basic syntax to more advanced concepts like functions and object-oriented programming. With these skills, you’re well-equipped to explore Python further and apply it to various fields such as web development, data analysis, and automation. Keep practicing and experimenting to deepen your understanding and enhance your programming abilities.