DEV Community

Cover image for Mastering the ABCs of Python fundamentals.
Insha Ramin
Insha Ramin

Posted on

Mastering the ABCs of Python fundamentals.

As developers, we are seekers of efficiency, readability, and versatility in our code. Python, with its elegant syntax and dynamic capabilities, has become the go-to choice for programmers across the globe.

In this first part of our Python series, we delve into the fundamental question: Why Python? What sets it apart, and why is it the language of choice for developers worldwide?

GIF

Why Python?

  • Clear and Readable Syntax: Python's syntax emphasizes readability and simplicity, fostering a codebase that is easy to understand and maintain.
  • Extensive Libraries and Frameworks: A rich ecosystem of libraries (e.g., NumPy, Pandas) and frameworks (e.g., Django, Flask) accelerates development across domains, from data science to web applications.
  • Cross-Platform Compatibility: Python's cross-platform compatibility ensures that code can be executed consistently across different operating systems.
  • Community Support: An active and vibrant community contributes to the language's evolution, fostering collaboration, and providing an extensive pool of resources.

Python professionals are in high demand, commanding competitive salaries. Whether in startups or established enterprises, Python proficiency opens doors to diverse roles, from backend development to data analysis.

As technology evolves, Python remains a linchpin, offering a career trajectory aligned with the dynamic demands of the modern tech landscape.

Setting Up Your Python Development Environment

Let's walk through the process, covering the installation of Python and the choice of a code editor or Integrated Development Environment (IDE).

  • Installing Python: Visit the official Python website (https://www.python.org/downloads/) and download the latest version suitable for your operating system (Windows, macOS, or Linux).

  • Once installed, open a terminal or command prompt and type python --version to verify the installation. You should see the installed Python version.

  • Choosing a Code Editor or IDE: You can pick any of your choice. Recommended ones are- VSCode, PyCharm, or Jupyter Notebooks.

Congratulations! You've successfully set up your Python development environment.

Alright! Time to delve into the basics starting from Variables and Data Types.

Variables and Data Types:

In Python, variables serve as dynamic containers, adapting to the nature of the data they hold.

Rules for variable names:

  1. Variable names can not start with a number
  2. There can be no spaces in the name, use _ instead.
  3. Names can not contain any of these symbols: :'",<>/?|!@#%^&*~-+
  4. Avoid using Python built-in keywords like list and str
  5. If there's a variable name with more than one word, you can use any of these
#Camel Case
myVariableName = "Peter"

#Pascal Case
MyVariableName = "Peter"

#Snake Case
my_variable_name = "Peter"
Enter fullscreen mode Exit fullscreen mode
  • Variable assignment follows name = object, where a single equals sign = is an assignment operator.
  • Variables can store data of different types, and different types can do different things.
name = "Insha"    # string
age = 21          # int
height = 5.4      # float
is_student = True # Boolean
Enter fullscreen mode Exit fullscreen mode
  • Variables do not need to be declared with any particular type, and can even change type after they have been set.
x = 4       # x is of type int
x = "Bob"   # x is now of type str
print(x)    # Output: Bob
Enter fullscreen mode Exit fullscreen mode
  • Python lets you reassign variables with a reference to the same object.
a = 10
a = a+10
print(a)    # Output: a = 20
Enter fullscreen mode Exit fullscreen mode
  • Variable names are case-sensitive.
y = 4
Y = "Popeye"   # Y will not overwrite y
Enter fullscreen mode Exit fullscreen mode

Determining variable type with type()

You can check what type of object is assigned to a variable using Python's built-in type() function. Common data types include:

int (for integer)
float
str (for string)
list
tuple
dict (for dictionary)
set
bool (for Boolean True/False)

Let's say we wan't to check the type of variable we create. Here's how we'll do it:

a = 4
print(type(a))   # Output: <class 'int'>

name = "Insha"
print(type(name)) # Output: <class 'str'>
Enter fullscreen mode Exit fullscreen mode

Numbers

Python has various "types" of numbers (numeric literals). We'll mainly focus on integers and floating point numbers.

Integers are just whole numbers, positive or negative. For example: 2 and -2 are examples of integers.

Floating point numbers in Python are notable because they have a decimal point in them, or use an exponential (e) to define the number. For example 2.0 and -2.1 are examples of floating point numbers.

x = 6    # int
y = 2.5  # float

print(type(x))      # prints the type of the variable
print(type(y))

Enter fullscreen mode Exit fullscreen mode

Type Conversion

You can convert from one type to another with the int(), and float(), methods:

x = 6    # int
y = 2.5  # float

#convert from int to float:
a = float(x)

#convert from float to int:
b = int(y)

print(a)      # Output: <class 'float'>
print(b)      # Output: <class 'int'>
Enter fullscreen mode Exit fullscreen mode

Arithmetic Operations in Python:

Now let's start with some basic arithmetic.

Addition (+)

# Example: Calculating the total cost of shopping items

item1_price = 20
item2_price = 30
item3_price = 15

total_cost = item1_price + item2_price + item3_price
print("Total Cost:", total_cost)      # Output: Total Cost: 55

Enter fullscreen mode Exit fullscreen mode

Subtraction (-)

# Example: Determining the change after a purchase

total_payment = 100
item_cost = 72

change = total_payment - item_cost
print("Change:", change)     # Output: Change: 28
Enter fullscreen mode Exit fullscreen mode

Multiplication (*)

# Calculating the area of a rectangular field

length = 10
width = 5

area = length * width
print("Area of the field:", area)  # Output: Area of the field:50
Enter fullscreen mode Exit fullscreen mode

Division (/)

# Distributing candies equally among friends

total_candies = 25
friends = 5

candies_per_friend = total_candies / friends
print("Candies per friend:", candies_per_friend)  # Output: Candies per friend: 5

Enter fullscreen mode Exit fullscreen mode

Floor Division (//) - The floor division ensures that only the whole number part is considered.

# Determining the number of full boxes needed for items

total_items = 50
items_per_box = 12

full_boxes = total_items // items_per_box
print("Full boxes needed:", full_boxes)   # Output: Full boxes needed: 4
Enter fullscreen mode Exit fullscreen mode

Modulus (%)

# Checking if a number is even or odd
user_input = 17

remainder = user_input % 2
if remainder == 0:
    print("The number is even.")
else:
    print("The number is odd.")

Enter fullscreen mode Exit fullscreen mode

Exponentiation ()

# Calculating compound interest
principal = 1000
rate = 0.05
time = 3

compound_interest = principal * (1 + rate) ** time
print("Compound Interest:", compound_interest)  # Output: Compound Interest: 1157.625
Enter fullscreen mode Exit fullscreen mode

These examples demonstrate how Python's arithmetic operations can be applied to solve real-world problems, from calculating costs to determining change and even checking if a number is even or odd.
Understanding these operations is foundational for working with numerical data in Python.

Wrapping Up

Excellent! Up to this point, we've discussed the fundamentals, including variables, data types, and numerical operations in Python.

In our upcoming article, we'll dive into the details of working with Strings in Python, revealing another facet of this powerful language.

Stay tuned! Happy Coding.

Top comments (0)