Posts

Showing posts from September, 2023

Iterations with Python

Iterations with Python Iterations with Python Iterations are a crucial concept in programming. We use them to execute repetitive code blocks. Python has provided us two types of iteration statements; the while loop and the for loop. Today let’s learn how these two can help us to avoid code repetition. while Loop We use while loops to repeat a code block multiple times as long as a particular condition is met or to repeat forever while the condition is True . Therefore, while loops may be used to validate user input. Let’s write a simple program to get user input and check if it is a positive integer. The variable is_positive_integer is initialized to True . This is used to check the while condition. If the user enters a digit and if it is larger than 0, is_positive_integer will be set to False . Therefore, while condition will stop execution. This is done with the help of if condition and isdigit() method. is_positive_integer = True while is_positiv...

Data Structures with Python

Data Structures with Python Data structures with Python Data structures are specific ways of arranging and storing collections of data, so that data can be organized and manipulated efficiently. Stack , Queue , Linked List and Tree are some of the most common language-independent data structures (Abstract data types). Moreover, every programming language has its own implementations of specific data structures. Python has four built-in data structures; List , Tuple , Set and Dictionary . So, today let’s compare and contrast these four data structures. List Lists are ordered (maintain the order of insertion), mutable (items can be modified after insertion) and indexed (can access elements based on position, i.e. index) collections of data. We can store data of the same type or of different types. Lists allow storing duplicate values. greetings = ["Hello", 123, 1.0] print(f"Before: {greetings}") greetings[1] = "Hola" greeting...

Conditionals with Python

Conditionals with Python Conditionals with Python Conditionals are used in programming to control the flow of a program based on different True of False conditions. Python traditionally provides if statements to serve this purpose. Moreover, with Version 3.10 , Python has provided us a switch-case equivalent which is the match statement. In this article, let’s talk about implementation of these two statements with a student grade evaluating program example. if Statement if statements generally use comparison operations to evaluate conditions. Additionally, we can use boolean operations to combine conditions. Let’s implement a simple program to decide student grade based on their marks. A mark from 75 to 100 would be evaluated for an ‘A’ grade. We use the and operation to combine the two conditions. if 100 >= marks and marks >= 75: print('A') However, Python provides a more succinct way of combining comparison conditions which is kn...

User-defined Functions with Python

User-defined Functions with Python User-defined Functions with Python Functions Functions are used in programming languages to organize code better and to provide code reusability. If we have to perform several tasks in our code, we can define functions for each of them separately. Then, we can reuse those functions in our main code when we need to perform the task repetitively. This way, we can abstract away the implementation details from our main code. Python provides a variety of built-in functions to make our lives easier. Further, Python provides us the capability of defining our own functions. In this article we will be learning how to define our own functions. User-defined Functions In our previous article we implemented a simple calculator program. Today let’s learn about functions and use them in our calculator program. def add(num1, num2): answer = round(num1 + num2, 2) return answer Above code snippet shows how we can define a function ...

Type Casting with Python

Type Casting with Python Type Casting with Python Built-in Types Every programming language comes with built-in data types. These help us to handle different types of data including numerical data and textual data. According to the official Python Standard Library documentation, Python has provided us many different Built-in Types . In this article we will only be discussing the Numeric Types “- int and float -” and the Text Sequence Type “- str -”. Type Conversion Type conversion -as the name implies- is converting data of one type to another. This either happens implicitly or can be done explicitly. Implicit type conversion is done by Python to convert smaller types to larger types. int < float < str Explicit Type Conversion (Type Casting) is done by the developer to convert larger types to smaller types. str > float > int Why Type Conversion? Let’s understand how each of these two works by creating a simple calculator program. In o...