DEV Community

nairobiHunter
nairobiHunter

Posted on

Python 101: Introduction to Python for Data Science

A python logo image

Python is a high-level, interpreted programming language which was first released in 1991 by Guido van Rossum.
It provides a simple, readable syntax, making it easy for developers to write and understand code. It is an interpreted language, meaning that the code is not compiled before running, but instead is executed line-by-line by a Python interpreter.
Python is a versatile language and can be used for many different purpose.

You might be wondering why does the logo has a snake('If your a curious person like myself) but the logo is from a comedy group called Monty which was popular in the 70s.The choice of the snake in the logo is also symbolic of the language's ease of use and flexibility. Python as a programming language is designed to be easy to learn and use, with a syntax that is simple and flexible.

Applications of Python

  • Web Development

  • Data analysis

  • Scientific computing

  • Machine learning

  • Artificial Intelligence

Benefits of using python

Python has a lot of application as it has a vast standard library and a large community of developers who create additional libraries and modules, providing a wide range of tools and functionality.

Python is benefits is that it is portable. Python code can be run on many different platforms, including Mac, Windows and Linux, which makes it a very accessible language compared to other languages.

Python's object-oriented design allows developers to organize code into reusable modules and classes, making it easier to write complex programs. It also support functional programming, which means that it is possible to write programs that use pure functions, making code easier to test and debug.

Python has a large and active community of developers who provide support resources,and libraries, making it a great language for both beginners and experienced developers.

It's also Open source, meaning it's a free language for everyone to interact with it.

Why python over other languages

In computer programming there syntax. Syntax refers to the rules that govern the structure and format of statements and commands in a programming language. In other words, it is the set of rules that determine how you can write code in a particular programming language.

Compared to many other programming languages, Python has a simpler and more intuitive syntax. Here are some key differences:

  • Readability:Python's syntax is designed to be easy to read and understand, with minimal use of symbols and special characters. This makes it easier to write that is clear and concise.

  • Flexibility: Python is a highly flexible language, which means that it can be used for a wide range of applications, from web development to scientific computing. Its simplicity and ease of use also make it a popular language for beginners.

  • Indentation: Unlike many other programming languages, Python uses indentation (whitespace at the beginning of a line) to indicate blocks of code. This helps to make code more readable and easier to follow.

  • Expressiveness: Python is known for its expressiveness, which means that it allows developers to write code that is shorter and more concise than many other languages. This is due to its use of high-level data structures and built-in functions.

  • Interpreted: Python is an interpreted language, meaning that code is executed line-by-line by a Python interpreter. This makes it easier to write and test code quickly, without the need for complex setup or compilation steps.

Overall, Python's syntax is designed to be simple, easy to read, and flexible, which makes it a popular choice for a wide range of programming applications.

HOW TO INSTALL PYTHON

Python is included with some operating system, but not all.
To check if you have python open the following command in terminal(Linux User's) it's also similar with Mac OS

A screenshot of linux terminal showing python is installed

From the above attachment I do have Python 3 installed but if you don't have python here's a link where you can download it https://www.python.org/downloads/ . If you're new to programming, there are many online resources and tutorials available to help you get started.

Start Coding

Before you can start to code, we did say that python is an interpreted programming language which means that as a developer you write Python files in a text editor and then put those files into the python interpreter to be executed, note that those files should be saved in (.py).

Let me guide you in creating your first file and writing your first code.To write a program in Python, you can use any text editor or integrated development environment (IDE). Here are the basic steps for writing a program in Python:

  • Open a text editor/ code editor or IDE.ie (Vscode)
  • Start a new file and save it with a .py extension (e.g., hello.py).

A screenshot of Vscode showing the file created

  • Write your Python code in the file.print('Hello, World')

An imagine displaying a line of code written on python

  • In Python, print() is a built-in function that is used to output text and other values to the console. The print() function takes one or more arguments and displays them in the console.
    In the example of print() function in Python to output the string "Hello, World!" to the console:

  • Save the file.

  • Open a terminal or command prompt.

  • Navigate to the directory where the file is saved.

  • Type python filename.py to run the program (replace filename with the name of your file). In our case we'll have "Hello.py"

  • The program will execute and output the result in the console.

If you did follow the steps correctly you'll have "Hello, World as the result.

A picture of Hello world

Congratulations, you have written and executed your first Python program.

DATA TYPES
Python is a dynamically typed language, which means that the data type of a variable is determined automatically based on the value that is assigned to it. Python supports several built-in data types that can be used to store and manipulate data. Let's take a closer look at each data type:

Integer (int):

  • integer is a numeric data type that represents whole numbers. It is a data type that is used to represent positive or negative numbers without any decimal points. Integers in Python are represented by the "int" keyword.Integers have unlimited precision, which means that they can be arbitrarily large or small.

Here are a few examples of integers in Python:

a picture of integers

In the example above, a and b are both integers that represent positive and negative numbers respectively. c is also an integer, but it represents the number zero.

You can perform mathematical operations on integers in Python such as addition, subtraction, multiplication, and division.
Arithmetic operators in Python are used to perform mathematical operations such as addition, subtraction, multiplication, division, and more. Here is a list of arithmetic operators in Python:

Addition (+): The addition operator is used to add two numbers together. For example, 2 + 3 equals 5.

Subtraction (-): The subtraction operator is used to subtract one number from another. For example, 5 - 2 equals 3.

Multiplication (*): The multiplication operator is used to multiply two numbers. For example, 2 * 3 equals 6.

Division (/): The division operator is used to divide one number by another. For example, 6 / 3 equals 2.

Floor Division (//): The floor division operator is used to divide one number by another and round down to the nearest whole number.Floor division in Python is a division operation that returns the largest possible integer value that is less than or equal to the result of the division. It is represented by the double forward slash operator (//) and is also known as integer division or truncating division.

For example, if you divide 7 by 2 using floor division, the result will be 3 because it is the largest integer that is less than or equal to the exact result, which is 3.5. Here's an example code snippet that demonstrates floor division in Python:

Image description of a code snippet that demonstrates floor division in Python

The output of this code will be: 3
Note that floor division always returns an integer value, even if the operands are floating-point numbers. For example, if you divide 8.0 by 3.0 using floor division, the result will be 2, not 2.6666. This is because floor division always rounds the result down to the nearest integer value.

Floor division is particularly useful when you want to divide two numbers and only need the integer part of the result. It can also be used in various programming tasks that require integer division, such as determining the number of times a given value can be subtracted from another value without going below zero.

Modulus (%): The modulus operator is used to return the remainder of a division operation. modulus operator is represented by the percent sign (%) and is used to return the remainder of a division operation. It gives the remainder of a division of one number by another number.

For example, if you divide 7 by 2, the quotient is 3 with a remainder of 1. The modulus operator can be used to get this remainder, like this:

Image description of modulus operator
The output of this code will be 1, which is the remainder of the division.

Here is another example that shows how modulus can be used in Python:

Image description of modulus operator

The output of this code will be 3, which is the remainder when 13 is divided by 5.

Modulus is often used in programming to check if a number is even or odd. When a number is divided by 2, the remainder will be 0 if the number is even, and 1 if the number is odd. Here's an example of how this can be done using modulus:

Image description of modulus operator
In this example, the output would be "x is odd" because 7 is an odd number.

Overall, the modulus operator in Python is a useful tool for performing operations that involve finding the remainder of a division operation.

Exponentiation (**): The exponentiation operator is used to raise a number to a power. For example, 2 ** 3 equals 8.

These operators can be used with numeric values, variables, or expressions that evaluate to numeric values.

For example:

An image of mathematical operations on integers in python

In the example above, we have used the "+" operator to add two integers, "-" operator to subtract two integers, "*" operator to multiply two integers, and "/" operator to divide two integers.

Float

  • In Python, a float is a numeric data type that represents decimal numbers. It is a data type used to represent numbers with a decimal point. Floats in Python are represented by the "float" keyword.

Here are a few examples of floats in Python:

A picture of float in python

In the example above, a and b are both floats that represent decimal numbers, and c is also a float that represents the number zero.

You can perform mathematical operations on floats in Python just like with integers. For example:

A picture of mathematical operations on floats in Python

In the example above, we have used the "+" operator to add two floats, "-" operator to subtract two floats, "*" operator to multiply two floats, and "/" operator to divide two floats.

It is important to note that due to the way that computers store floating-point numbers, there can be some rounding errors or inaccuracies when performing operations on them. This is something to keep in mind when working with floats in Python or any other programming language.

String

  • Strings are used to represent text. In Python, strings are enclosed in either single or double quotes. Strings are immutable, which means that once they are created, their values cannot be changed.Strings are used to represent textual data, such as names, addresses, place or other forms of human-readable data.

Image of strings in python

In the example above, name and address are both strings that represent textual data, and message is also a string that contains a greeting.

List

  • Lists are used to represent ordered collections of values. Lists can contain values of different data types, and they can be indexed and sliced. In Python, lists are created using square brackets. Here are some examples:

Image of lists in python

Tuple

  • Tuple is an ordered, immutable sequence of elements. Tuples are similar to lists, but the main difference is that tuples are immutable, which means that their elements cannot be modified once they are defined. Tuples are defined using parentheses (), and the elements are separated by commas.Tuples are often used to represent fixed sequences of values. In Python, tuples are created using parentheses. Here are some examples:

Image description of a tuple in python

Once you have defined a tuple, you can access its elements using indexing, just like you would with a list. For example, to access the second element of the tuple, you would use:

Image description of a tuple in python

Tuples can be useful in situations where you want to store a collection of values that should not be modified, such as a set of constants or configuration parameters. Tuples are also often used as return values from functions, since they allow you to return multiple values as a single object.

Booleon

  • Booleans are used to represent truth values, which can be either True or False. In Python, boolean values are case-sensitive and should be capitalized. Boolean values are often used in conditional statements and loops to control the flow of a program based on the outcome of a test. For example, you might use a Boolean value to check whether a condition is true or false, like this:

Image description of booleon in python

In this example, the > operator tests whether x is greater than y, and the result is assigned to the is_greater variable. Since x is not greater than y, the result is False.

Boolean values are a fundamental building block of programming logic and are used extensively in all kinds of programming tasks, from simple conditionals to complex algorithms.

Dictionary:

  • Dictionaries are used to represent collections of key-value pairs. Dictionary is an unordered collection of key-value pairs, also known as an associative array or a hash table. Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be any immutable type, such as strings, numbers, or tuples. In Python, dictionaries are created using curly braces. Keys must be unique, and values can be of different data types.Key-value pairs are separated by a colon.Here are some examples:

Image description of Dictionary in python

In this example, the keys are 'name', 'age', and 'city', and the values are 'Hunter', 12, and 'New York', respectively. You can access the value associated with a key by using square brackets [] and the key name.

Set

  • Sets are used to represent collections of unique values. Set is an unordered collection of unique elements. Unlike lists and tuples, sets do not allow duplicate elements, and the order of the elements is not defined. In Python, sets are created using curly braces or the set() function.Here are some examples:

Image description of sets in python

You can also define a set from a list, like this:

Image description of set in python

In this example, the set constructor removes the duplicate elements and creates a new set with only the unique elements.

You can perform various operations on sets, such as union, intersection, and difference. For example, to create a union of two sets, you can use the | operator, like this:

Image description of sets in python

In this example, the union_set contains all the elements that are in either set1 or set2.

Sets are often used in Python to perform operations on collections of elements, such as finding unique values, removing duplicates, or checking for intersections between sets.

These are the most common data types in Python. Understanding how to work with these data types is essential for writing effective and efficient Python code.

That's it for Python 101 class, I did leave out other stuff but this is only the start.
if you found this post helpful don't forget to like, save and follow for more.

Top comments (0)