Sitemap

Python Through Examples

5 min readOct 10, 2024

--

Press enter or click to view image in full size

Introduction

Python is a key milestone in the data science journey. Its syntax is one of the simplest among programming languages. So, if you are new to programming or want to excel data science, Python is the best first step. I am associated with Data Seekho’s DSMP (Data Science Mastery Program), which provides a structured approach to learning Python and data science fundamentals with practical exercises. In this blog, I will present some fundamentals of Python with examples.

Comments in Python

In Python, comments are short explanations of the code. They explain the logic of each step in programming. Programmers write comments to understand the program and use it later. The interpreter does not execute the comments in the program. There are two types of comments.

  • Single Line Comment

Single line comments are started with ‘#’. Anything written after ‘#’ in a single does not display on output.

  • Multiline Comments

Triple quotes are used for multiline comments. Anything written between triple quotes is not executed in the program.

Rules for Variable Names

· Variables start with a letter or underscore (e.g., ‘books’, ‘_num’)

· Variables cannot start with a number (e.g., ‘1number’ is invalid)

· Numbers can be used in the middle of a name (e.g., ‘num1ber’)

· Special characters cannot be used at the start or end (e.g., ‘$num’ or ‘num$’ is invalid)

· Spaces are not allowed (e.g., ‘my name’ is invalid)

· Variables are case sensitive (Name, name)

· Cannot concatenate/ add string with other data types, we use str() method to convert other data types into string

Tip: meaningful whole name are more helpful than random words

Descriptive Variable Names

Let’s consider we have simple variable names: a = 5 b = 10 c = a * b. We have to replace these names with meaningful names.

Press enter or click to view image in full size

Types of Errors

  1. Syntax Error

The syntax error occurs when programmers do the violation of the syntax of Python. Interpreters detect the error when programmers write the program. This error occurs in following conditions:

  • Missing Colon(:)
  • Missing Quotes (‘’)
  • Missing Indentation/Space ( )
  • Unmatched Parentheses{]
  • Using Keywords as Variables

Correction of Errors

Press enter or click to view image in full size

2. Runtime Error

Runtime error occurs when programmers follow the syntax of python but error occurs during the execution of the program. This error occurs in the following conditions:

  • Name Error: Access Undefined Variable
  • Type Error: When a function is applied to the wrong data type
  • ZeroDivision Error: when divide a number by zero
  • Index Error: This error occurs when the programmers try to access the index of a sequence out of range. The interpreter detects it easily.

Correction of Runtime Error

Press enter or click to view image in full size

3. Logical Error

Get Sobia Noor’s stories in your inbox

Join Medium for free to get updates from this writer.

Logical error occurs for using wrong logic in programming. The syntax of the program is right and it runs smoothly but it provides wrong answers. This error is difficult to detect by the interpreter. This error occurs in the following conditions:

  • Incorrect Order of Operation
  • Arithmetic Error

Correction of Logical Error

Arithmetic Operations

  • Addition(+)
  • Subtraction(-)
  • Multiplication(*)
  • Division(/)
  • Floor Division(//)
  • Modulus(%)
  • Exponentiation(**)

Arithmetic Operation on Other Data types

Simple Calculator in Python

To create a calculator in Python, we take two inputs and an operation (+, -, *, /) from the user. Then, we use `if-else` statements based on the user’s selection of the operation. If the user selects addition, the program prints the result of adding the two numbers. If the user selects subtraction, it prints the result of subtracting the two numbers. If the user selects multiplication, it ignores the previous conditions and prints the result of multiplying the two numbers. Similarly, if the user selects division, it shows the result of the division.

For division, the denominator should not be zero; otherwise, it will raise a “zero division” error. Additionally, if the user inputs invalid data, like a string, the program will display a notification asking them to enter valid input.

Some Built-in Functions for Calculator

Min()

This function finds the value

  • Min_value = min (1, 3, 10)

Max()

  • This function finds the max value
  • Max_value = min (1, 3, 10)

Abs()

  • This function finds absolute/positive value
  • abs_value = abs (-1, -3, 10)

Append()

  • This function adds new value to the list
  • Mylist.append (‘mango’)

Input()

  • This function gets values from the user.
  • Input(‘Enter your marks’)

Str()

  • By default input() function gets values as strings. It means we must convert other data types into strings by the str() function.
  • Str(Input(‘Enter your marks’))

Range()

  • This function set the range of a loop
  • Range(5)

For Loop

  • It is used to repeat code in a loop
  • Syntax : for i in range(5): Statement
  • Where i is used for increment in turn

Calculator

Output

Press enter or click to view image in full size

Application of Built-in Functions

Source Code on GitHub

https://github.com/SobiaNoorAI/Data-Seekho-Data-Science-Mastery-Program/blob/main/Module%201%3A%20INTRODUCTION%20TO%20DATA%20SCIENCE/Assignment_no1_Data_Seekho_Student_Notebook.ipynb

Conclusion:

In this blog, We learn about comments, types of errors and arithmetic operations. Although, there are many subtypes of errors and functions. However, we discuss few that are more relevant. Finally, we create a simplest calculator as a final project in this blog.

--

--

Sobia Noor
Sobia Noor

Written by Sobia Noor

Enthusiast in AI, robotics, Data Science