DEV Community

Cover image for Why Python ?
leslie angu
leslie angu

Posted on

Why Python ?

Python was the first programming language that I learnt. But I didn't know how far it had come and how its being used in different industries from ecommerce, logistics to firmware design.

What makes python an easy language for most beginners?

  1. Python has a simple syntax that is similar to the English language.
  2. Python has a wide range of libraries that perform arithmetic, machine learning deployment to web applications.
  3. Python has a large community of developers that develop and maintain these libraries.

What is the basic python syntax?

The most fundamental function in python that is written by most programmers is print()
print("Hello World")

What does print() function do? It sends data to the standard output device which is usually your terminal or console device.
The variable
A variable is a named storage location in a computer's memory that acts as a temporary container to hold data while program is running.
A variable relies on 3 key properties:

  1. The name(identifier): the text label you use to reference the data in your code.
  2. The value: the actual data stored inside the variable.
  3. The data type: the classification of the data. In python we have 4 kinds of data types: Integer, Float/Double, String(str), and Boolean(bool).

The Integer(int) is a whole number eg:
Age = 25

The Float/Double is a decimal number eg:
Height=1.50

The String(str) is a text eg:
greeting="Hello World"

Boolean(bool) is True or False values:
has_arrived=True

The equal sign (=) acts as the assignment operator, taking the value on the right side and packing it neatly into the variable name on the left.

Operators
An operator is a specific symbol or keyword that tells the computer to perform a distinct mathematical, relational or logical manipulation on data.
We have 4 common types of operators in python: Arithmetic, Assignment, Comparison/Relational and logical.

Arithmetic
It performs basic math calculations:
+, -, *, /, %

a = 150
b = 200
c = 300
d = 10
e = 3
total = a + b + c -> 650
diff = b - a -> 50
rem = d % e -> 1
div = c / d -> 100

Enter fullscreen mode Exit fullscreen mode

Assignment
Assigns or updates values in a variable:
=, +=, -=, *=
score += count -> score = score + count

Comparison/ Relational
Compares multiple conditions together:
and, or, not

if age = 18 and id_present = True:
      print("You can drive")
Enter fullscreen mode Exit fullscreen mode

Loops
A control flow structure used to execute a block of code repeatedly as long as specified condition is met.

Why are loops used?

  • To reduce redundancy: you don't have to manually type repetitive statements.
  • To save time and space: It keeps your codebase clean, short and highly efficient.
  • To handle dynamic data: Loops allow you to process massive lists or user inputs of any size without rewriting your code.

for loop: When you know exactly how many times you need to repeat the action:

items = ['sugar', 'salt', 'flour', 'soap', 'oil']
for i in items:
    print(i)
# output
sugar
salt
flour
soap
oil
Enter fullscreen mode Exit fullscreen mode

while loop: When the number of repitions is unknown and depends on a condition staying true.

while True:
   if i < 10:
       i+=1
       break

Enter fullscreen mode Exit fullscreen mode

functions : this is a self-contained, reusable block of code designed to perform a specific, isolated task. Instead of writing the same instructions over and over again, you package those instructions inside a function, give it a meaningful name, and trigger it whenever you need it.

def sum(a, b):              # declaration & parameters
     sum = a + b            # processing (body)
     return sum             # return value
total = sum(100, 200) -> 300     # calling the function

Enter fullscreen mode Exit fullscreen mode
  • The name & parameters(inputs): the variable inside the parenthess(a, b) act as placeholders for the data you pass into the function.
  • The body: The lines of code indented below the name that execute the actual math, logic or action
  • The retutn value: The return statement sends the final result back to the main program and stops the function.
  • The function call : Typing the function's name followed by parentheses sum(100, 200) tells the computer to jump to that code block, run it and bring back the answer.

Conclusion

When a programmer has an idea, it is written down as pseudocode that can be translated into a programming language such as python. Because of the simplicity that python has, most programmers use it since it takes the least amount of time and code to write it.

Top comments (1)

Collapse
 
josephine_mackylah_d6b31f profile image
Josephine Mackylah

Great breakdown... especially the "why" behind Python's simplicity