
seen from United States
seen from Malaysia
seen from South Korea
seen from China
seen from China
seen from China

seen from Brazil
seen from China

seen from United States

seen from United States
seen from T1
seen from France

seen from Singapore

seen from Germany
seen from Cyprus
seen from South Korea
seen from United States
seen from South Korea

seen from United States

seen from Germany
How to Check if a Given Number is Fibonacci number - Python
Fibonacci numbers are part of a famous sequence where each number is the sum of the two preceding ones, i.e. F(n) = F(n-1) + F(n-2). The sequence starts as:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
Notice that every number is equal to the sum of its previous 2 numbers.
In this article, we will learn how to identify if a given number belongs to the Fibonacci series or not.
Examples :
Input: 8 Output: Yes Input: 31 Output: No
Fibonacci Number Check Using a Mathematical Property
A number n is a Fibonacci number if and only if one or both of (5*n² + 4) or (5*n² – 4) is a perfect square.
The above mathematical expression is derived from the closed-form expression of Fibonacci numbers (Binet’s Formula) and some number theory. It’s fast and doesn’t require generating the Fibonacci sequence. Let's look at the code implementation in Python:import mathdef is_perfect_sq(x): s = int(math.sqrt(x)) return s * s == xdef is_fibonacci(n): return is_perfect_sq(5 * n * n + 4) or is_perfect_sq(5 * n * n - 4)for i in range(1, 7): if is_fibonacci(i): print(f"{i} is a Fibonacci Number") else: print(f"{i} is not a Fibonacci Number")
Output1 is a Fibonacci Number 2 is a Fibonacci Number 3 is a Fibonacci Number 4 is not a Fibonacci Number 5 is a Fibonacci Number 6 is not a Fibonacci Number
Explanation:
1. is_perfect_sq(x):
Calculates the integer square root of x.
Returns True if x is a perfect square, else False.
2. is_fibonacci(n):
Applies the mathematical identity:
A number n is Fibonacci if 5*n² + 4 or 5*n² – 4 is a perfect square.
Calls is_perfect_sq() on both expressions to check this.
3. Loop: Iterates through numbers 1 to 6 and prints whether each number is a Fibonacci number based on the result from is_fibonacci(). 👉 Follow Ashok IT for daily Python logic programs
What Is Random Number Generator in Python and How to Use It
Random numbers play a crucial role in programming and real-world applications such as games, simulations, cryptography, data science, testing, and machine learning. Python makes working with random numbers simple and efficient through its built-in random module.
If you are new to Python, you might wonder: What is a random number generator in Python, and how do we use it?
This article explains the concept of random number generation, how Python generates random numbers, and how to use different random functions with clear examples.
What Is a Random Number Generator?
A Random Number Generator (RNG) is a system that generates a sequence of numbers that appear to be random.
In Python:
Random numbers are pseudo-random, not truly random
They are generated using mathematical algorithms
The sequence can be reproduced using a seed value
In simple terms:
Python’s random number generator produces values that behave like random numbers for practical use.
Random Number Generator in Python
Python provides the random module to generate random numbers and perform random operations.
Importing the random Module
import random
Once imported, you can access various random functions.
Generating Random Integers
The most commonly used function is randint().
Syntax
random.randint(start, end)
Example
import random num = random.randint(1, 10) print(num)
✔ Generates a random integer between 1 and 10 (inclusive).
Generating Random Floating-Point Numbers
Use random() to generate random float values.
Example
import random value = random.random() print(value)
✔ Generates a float value between 0.0 and 1.0.
Generating Random Float in a Range
Use uniform() to generate a float within a specific range.
Example
import random value = random.uniform(10, 20) print(value)
✔ Generates a float between 10 and 20.
Selecting Random Elements
Using choice()
import random colors = ["red", "blue", "green", "yellow"] print(random.choice(colors))
✔ Randomly selects one element from a list.
Generating Multiple Random Values
Using sample()
import random numbers = [1, 2, 3, 4, 5] print(random.sample(numbers, 3))
✔ Selects 3 unique random elements.
Shuffling Elements Randomly
Using shuffle()
import random cards = [1, 2, 3, 4, 5] random.shuffle(cards) print(cards)
✔ Randomly rearranges elements in a list.
Using Random Seed
A seed controls the randomness sequence.
Example
import random random.seed(10) print(random.randint(1, 100))
✔ Same seed → same output ✔ Useful for testing and debugging
Random Number Generator Functions Summary
FunctionDescriptionrandom()Random float (0.0 to 1.0)randint(a, b)Random integeruniform(a, b)Random floatchoice(seq)Random elementsample(seq, k)Random sampleshuffle(seq)Shuffle sequenceseed(x)Set randomness
Real-World Applications of Random Numbers
Python RNG is used in:
Games and simulations
Password generation
Data sampling
Machine learning
Testing and automation
Statistical modeling
Is Python’s Random Generator Truly Random?
No. Python uses pseudo-random algorithms, which means:
Results look random
Sequence can be reproduced
Suitable for most applications
For cryptographic security, Python provides the secrets module.
Common Beginner Mistakes
Forgetting to import the random module
Confusing randint() and random()
Expecting true randomness
Using random for security purposes
Random Number Generator Interview Questions
What is a random number generator?
How does Python generate random numbers?
Difference between randint() and random()?
What is a seed in Python?
Is Python RNG secure?
Best Practices
Use random for general purposes
Use secrets for security-related tasks
Set seeds for reproducible results
Avoid relying on randomness for critical logic
The Random Number Generator in Python allows developers to generate numbers and values that simulate randomness for a wide range of applications. Using the built-in random module, you can generate integers, floats, random elements, shuffled lists, and reproducible results with ease.
Understanding how to use random numbers correctly is essential for Python beginners, DSA learners, data scientists, and interview preparation. Once mastered, randomness becomes a powerful tool in your Python programming toolkit.
Python For Loop Tutorial With Examples To Practice
Loops are a fundamental concept in programming that allow you to execute a block of code repeatedly. In Python, the for loop is one of the most commonly used loops because it is simple, readable, and powerful.
Whether you are a beginner learning Python or preparing for coding interviews, understanding the Python for loop is essential. This tutorial explains the syntax, working, examples, and practice problems to help you master the for loop in Python.
What Is a For Loop in Python?
A for loop in Python is used to iterate over a sequence such as:
List
Tuple
String
Dictionary
Set
Range
In simple words:
A for loop repeats a block of code for each element in a sequence.
Syntax of Python For Loop
for variable in sequence: statements
variable → Takes the value of each element
sequence → Collection or range
statements → Code executed in each iteration
Basic Example of For Loop
for i in range(5): print(i)
Output
0 1 2 3 4
How Python For Loop Works
The loop picks the first element from the sequence
Executes the block of code
Moves to the next element
Repeats until the sequence ends
For Loop Using range()
The range() function is commonly used with for loops.
Example
for i in range(1, 6): print(i)
Output
1 2 3 4 5
For Loop With List
languages = ["Python", "Java", "C++"] for lang in languages: print(lang)
Output
Python Java C++
For Loop With String
for ch in "Python": print(ch)
Output
P y t h o n
For Loop With Tuple
numbers = (10, 20, 30) for n in numbers: print(n)
For Loop With Dictionary
student = {"name": "Alice", "age": 21} for key, value in student.items(): print(key, value)
Nested For Loop
A nested for loop means a loop inside another loop.
Example
for i in range(1, 4): for j in range(1, 3): print(i, j)
Output
1 1 1 2 2 1 2 2 3 1 3 2
For Loop With break
The break statement stops the loop immediately.
for i in range(1, 6): if i == 4: break print(i)
For Loop With continue
The continue statement skips the current iteration.
for i in range(1, 6): if i == 3: continue print(i)
For Loop With else
The else block executes after the loop finishes normally.
for i in range(3): print(i) else: print("Loop completed")
Practice Examples for Python For Loop
1. Print Numbers From 1 to 10
for i in range(1, 11): print(i)
2. Find Sum of Numbers
total = 0 for i in range(1, 6): total += i print(total)
3. Print Even Numbers
for i in range(1, 21): if i % 2 == 0: print(i)
4. Multiplication Table
num = 5 for i in range(1, 11): print(num, "x", i, "=", num * i)
5. Count Characters in a String
count = 0 for ch in "Python": count += 1 print(count)
Common Beginner Mistakes
Forgetting indentation
Using wrong range values
Confusing for with while
Modifying the sequence inside loop
Time Complexity of For Loop
The time complexity depends on:
Number of iterations
Operations inside the loop
Usually:
O(n) for simple loops
For Loop Interview Questions
What is a for loop in Python?
Difference between for loop and while loop?
What is range()?
Can for loop be used with else?
Difference between break and continue?
When to Use For Loop
Iterating over sequences
Repeating tasks fixed number of times
Processing data collections
Writing clean and readable code
The Python for loop is one of the most powerful and beginner-friendly looping constructs. It helps you iterate over data efficiently, write clean code, and solve real-world programming problems.
By practicing the examples in this tutorial, you will gain confidence and build a strong foundation in Python programming. Mastering for loops is a crucial step toward learning advanced Python concepts and cracking coding interviews.