Building Blocks of Algorithm in Python5 Jan 2025 | 3 min read Sequence, selection, and iteration are the fundamental building elements from which algorithms may be built. Statements:In a computer, a statement is one action. Statements in a computer might contain some of the following operations:
StateState is the transition, during a certain time interval, from one process to another under specified conditions. Control Flow:Control flow is the process of carrying out each statement in a predetermined order. There are three approaches to implementing the control:
Sequence:Sequence execution is the process of carrying out each instruction one after the other. This building block entails specifying a set of actions that must be carried out in a particular order. Usually, sequences are employed for simple, linear operations. For instance, you would construct a series that adds two integers to find their sum. For Example: Add two numbers: Step 1: Start Step 2: Get a, b Step 3: Calculate c = a + b Step 4: Display c Step 5: Stop Selection:A selection statement transfers program control, condition-based, to a particular section of the program. One portion of the program will run if the conditional test is true; if not, the other portion of the program will run. An algorithm may decide depending on conditions, thanks to selection. The 'if' statement is the most used selection construct. It allows you to run distinct code blocks based on the truth value of a given condition. For example, you can use 'if' to check if a given integer is odd or even. ![]() For Example: Write an algorithm to check whether he is eligible to vote. Step 1: Start Step 2: Get age Step 3: If age >= 18, print "Eligible to vote" Step 4: Else print "Not eligible to vote" Step 5: Stop Iteration:Certain statements are repeatedly run in some programs depending on conditional tests. i.e., carried out repeatedly. We refer to this kind of operation as looping or iteration. Iteration is the process of repeating a set of instructions several times, frequently with the use of Python loops like "for" and "while." Loops are essential for processing data structures or performing repeating operations. For instance, you may execute the same action on each item in a list by iterating across the list. For Example: Write an algorithm to print all natural numbers up to n Step 1: Start Step 2: Get n value. Step 3: Initialize i = 1 Step 4: If (i <= n), go to step 5 else go to step 7 Step 5: Print i value and increment i value by 1 Step 6: Go to step 4 Step 7: Stop Functions:
Advantages of Function Use
For Example: Algorithm for addition of two numbers using function Main function () Step 1: Start Step 2: Call the function add () Step 3: Stop sub-function add () Step 1: Function start Step 2: Get a and b values Step 3: add c = a + b Step 4: Print c Step 5: Return ![]() |
In Python, everything is an object, and objects have attributes. Attributes are essentially the characteristics or properties associated with an object. These attributes can be variables, methods, or even other objects. Understanding attribute lists in Python is pivotal to harness the language's versatility and power. What...
4 min read
Python has become a back-end development powerhouse in today's tech-driven world. It's the perfect solution for developing reliable back-end systems that power websites, web apps, and APIs because of its simplicity, adaptability, and readability. This thorough article will take you through the necessary steps to...
6 min read
Introduction: In number theory and cryptography, prime numbers are crucial. Numerous techniques have been created for the goal of identifying prime numbers, which is essential in many applications. The Lucas primality test is one such algorithm, and it provides a quick way to tell whether a...
3 min read
? CSV files are structured as a table with rows and columns. Each line in the file represents a record, and a comma separates each value. The first row is usually reserved for column headers, which describe the content of each column. It is essential to...
4 min read
An Introduction to Short-Circuiting Short circuiting makes the evaluation of logical expressions better. When you're dealing with if-statements or tricky true/false logic, you often use "and", "or", and "not" to mix different conditions. But in many cases, you can figure out the final result of a...
6 min read
? Matplotlib is a robust Python charting toolkit frequently used to create visuals. Occasionally, it may be necessary to plot several figures in a single window, but sometimes, you might need to display them individually. This could be helpful for structuring intricate visualisations or comparing various...
4 min read
Guido Van Rossum, the mastermind behind the Python programming language, once explained that Python is a playground for programmers. It's a delicate balance between giving them enough freedom to be creative without making the code unreadable. That's one reason Python has become incredibly popular-it strikes...
16 min read
? Pomegranate could be a solid Python probabilistic modeling library that's especially valuable for working with hidden Markov models, Bayesian networks, and other factual models. Whether you're working on machine learning, data science, or statistical analysis, Pomegranate gives a capable collection of tools for modeling complex connections...
4 min read
Introduction: Python's random.gauss(mu, sigma) function produces a random number with a given mean (mu) and standard deviation (sigma) that follows a Gaussian (normal) distribution. To produce data that mimics actual random changes, statistical modelling and simulations frequently use this technique. This tutorial will provide an in-depth understanding of...
3 min read
Visualising the data is an essential component while analysing huge amounts of data. Python offers a number of libraries and functions for the same, which helps create simple but interactive graphs and charts. Matplotlib is a widely used and most common library for this purpose....
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India