DEV Community

Owoeye Anjolaoluwa Khalifa
Owoeye Anjolaoluwa Khalifa

Posted on

Basic Fundamentals In Python

Python is a programming language loved by so many data scientists, web developers, and even software engineers. To master Python for any profession, you have to master the fundamentals of the Python programming language. So if you want to learn the fundamentals of the Python programming language, this article is for you. In this article, I’m going to be explaining to you the basic fundamentals in Python that you need to know before you jump into data science, web development, software development or any field that interests you.

Basic Fundamentals In Python:
This are some basic fundamentals NOT ALL to know before going in some python majors like web development, data science, software development and so on.

Data Types in Python:

Data types make programming easier. A data type is a set of values and a set of operations defined on data. An implementation of a data type is an expression of data and operations in terms of a specific programming language. So, here are the data types in Python that you should know before you start learning the Python programming language:

Data Types Examples

Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool

Lists:

Lists are ordered collections of items and the most general data type provided by the Python programming language. They are mutable, which means that items stored in a list can be edited. A list is typically used to perform operations on a collection of items at a time.

mylist = ["apple", "banana", "cherry"]
Enter fullscreen mode Exit fullscreen mode

Tuples:

A tuple is also a collection of items very similar to a list in Python. Like lists, they allow you to store an ordered collection of items to perform operations at one time. But unlike lists, a tuple cannot be modified once created. The only advantage of using tuples over lists is that tuples are slightly faster than lists.

mytuple = ("apple", "banana", "cherry")
Enter fullscreen mode Exit fullscreen mode

Dictionary:

A dictionary in Python is completely different from lists and tuples, they are not sequences but mappings. Mappings are also collections of items but in the form of key and value pairs. Simply put, a dictionary contains indexes with keys mapped to certain values.

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
Enter fullscreen mode Exit fullscreen mode

If Else Statements:

If-else statements are conditional statements found in all programming languages. These statements are used to write an event-driven program. Simply put, these statements are used when we want to execute a set of statements only if the given condition is met.
Mastering all this before the If Else Statements code is very important:
Equals: a == b
Not Equals: a != b
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b

If statement:

a = 33
b = 200
if b > a:
print("b is greater than a")
return

b is greater than a
Enter fullscreen mode Exit fullscreen mode

In this example we use two variables, a and b, which are used as part of the if statement to test whether b is greater than a. As a is 33, and b is 200, we know that 200 is greater than 33, and so we print to screen that "b is greater than a".

Indentation

Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose.

Example
If statement, without indentation (will raise an error):

a = 33
b = 200
if b > a:
print("b is greater than a") # you will get an error

Elif
The elif keyword is pythons way of saying "if the previous conditions were not true, then try this condition".

Example
a = 33
b = 33
if b > a:

print("b is greater than a")
elif a == b:
  print("a and b are equal")
Enter fullscreen mode Exit fullscreen mode

In this example a is equal to b, so the first condition is not true, but the elif condition is true, so we print to screen that "a and b are equal".

Else
The else keyword catches anything which isn't caught by the preceding conditions.

Example
a = 200
b = 33

if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
else:
  print("a is greater than b")
Enter fullscreen mode Exit fullscreen mode

In this example a is greater than b, so the first condition is not true, also the elif condition is not true, so we go to the else condition and print to screen that "a is greater than b".

Loops:
Loops are statements used to iterate over an object. There are two types of loops in Python:

While Loops
For Loops
A for loop is used to iterate through a collection of items. The While Loop allows you to execute a set of instructions until the given condition is true.

while loops:

Example
Print i as long as i is less than 6:

i = 1
while i < 6:
  print(i)
  i += 1
Enter fullscreen mode Exit fullscreen mode

Note: remember to increment i, or else the loop will continue forever.

The while loop requires relevant variables to be ready, in this example we need to define an indexing variable, i, which we set to 1.

The break Statement
With the break statement we can stop the loop even if the while condition is true:

Example
Exit the loop when i is 3:

i = 1
while i < 6:
  print(i)
  if i == 3:
    break
  i += 1
Enter fullscreen mode Exit fullscreen mode

The continue Statement
With the continue statement we can stop the current iteration, and continue with the next:

Example
Continue to the next iteration if i is 3:

i = 0
while i < 6:
  i += 1
  if i == 3:
    continue
  print(i)
Enter fullscreen mode Exit fullscreen mode

The else Statement
With the else statement we can run a block of code once when the condition no longer is true:

Example
Print a message once the condition is false:

i = 1
while i < 6:
  print(i)
  i += 1
else:
  print("i is no longer less than 6")

Enter fullscreen mode Exit fullscreen mode

For loops:
Example
Print each fruit in a fruit list:

fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
The for loop does not require an indexing variable to set beforehand.

Looping Through a String
Even strings are iterable objects, they contain a sequence of characters:

Example
Loop through the letters in the word "banana":

for x in "banana":
  print(x)
Enter fullscreen mode Exit fullscreen mode

The break Statement
With the break statement we can stop the loop before it has looped through all the items:

Example
Exit the loop when x is "banana":

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)
  if x == "banana":
    break
Enter fullscreen mode Exit fullscreen mode

Example
Exit the loop when x is "banana", but this time the break comes before the print:

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  if x == "banana":
    break
  print(x)
Enter fullscreen mode Exit fullscreen mode

The continue Statement
With the continue statement we can stop the current iteration of the loop, and continue with the next:

Example
Do not print banana:

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  if x == "banana":
    continue
  print(x)
Enter fullscreen mode Exit fullscreen mode

The range() Function
To loop through a set of code a specified number of times, we can use the range() function,
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.

Example
Using the range() function:

for x in range(6):
  print(x)
Enter fullscreen mode Exit fullscreen mode

Note that range(6) is not the values of 0 to 6, but the values 0 to 5.

The range() function defaults to 0 as a starting value, however it is possible to specify the starting value by adding a parameter: range(2, 6), which means values from 2 to 6 (but not including 6):

Example
Using the start parameter:

for x in range(2, 6):
  print(x)
Enter fullscreen mode Exit fullscreen mode

The range() function defaults to increment the sequence by 1, however it is possible to specify the increment value by adding a third parameter: range(2, 30, 3):

Example
Increment the sequence with 3 (default is 1):

for x in range(2, 30, 3):
  print(x)
Enter fullscreen mode Exit fullscreen mode

Else in For Loop
The else keyword in a for loop specifies a block of code to be executed when the loop is finished:

Example
Print all numbers from 0 to 5, and print a message when the loop has ended:

for x in range(6):
  print(x)
else:
  print("Finally finished!")
Enter fullscreen mode Exit fullscreen mode

Note: The else block will NOT be executed if the loop is stopped by a break statement.

Example
Break the loop when x is 3, and see what happens with the else block:

for x in range(6):
  if x == 3: break
  print(x)
else:
  print("Finally finished!")
Enter fullscreen mode Exit fullscreen mode

Nested Loops
A nested loop is a loop inside a loop.

The "inner loop" will be executed one time for each iteration of the "outer loop":

Example
Print each adjective for every fruit:

adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]

for x in adj:
  for y in fruits:
    print(x, y)
Enter fullscreen mode Exit fullscreen mode

The pass Statement
for loops cannot be empty, but if you for some reason have a for loop with no content, put in the pass statement to avoid getting an error.

Example

for x in [0, 1, 2]:
  pass
Enter fullscreen mode Exit fullscreen mode

Functions:
Python has a lot of built-in functions like print, input, etc. Besides the built-in functions of Python, you can also define your functions. A function is a block of code defined to perform a certain action. They are primarily used to replace repetitive statements in your code.
Example of python built in function is the print, step on how to use the 'print function'.

print("Hello, World!")
printing this code will return

Hello, World!
Enter fullscreen mode Exit fullscreen mode

Calling a Function
To call a function, you will use the function name followed by parenthesis:

Example
def my_function():
print("Hello from a function")
my_function()

it will return

Hello from a function
Enter fullscreen mode Exit fullscreen mode

Summary
So these were all the fundamental concepts that you should know before starting with learning Python for Data Science, Web Development or any other profession. Learn these fundamentals of Python is very important to master Python for any profession that interests you. I hope you liked this article on the fundamentals of Python. Feel free to ask your valuable questions in the comments section.

Top comments (3)

Collapse
 
jwp profile image
John Peters

Tx

Collapse
 
mopharr profile image
Mopharr

Good one boss

Collapse
 
corentinzrt profile image
Corentin-zrt

Nice long explaination of basics of python. I liked it. 👍