Sharing notes from my ongoing learning journey — what I build, break and understand along the way.
Python Basics Part 5: Random Numbers, f-Strings, and Input/Output Explained
Python Basics – Part 5: Random Numbers, String Formatting, and Input/Output
In this lesson from my 5-week Python training series, we’ll explore three practical topics that bring programs to life:
random number generation, formatted strings (f-strings), and user input/output.
We’ll also end with an introduction to data visualization using Matplotlib.
Random Numbers in Python
Python’s random module provides a variety of functions for generating pseudo-random numbers.
Let’s start with the basics.
random()
Returns a float between 0 (inclusive) and 1 (exclusive).
from random import random
number = random()
print(number)
Example Output:
0.453443
randint(a, b)
Returns an integer between a and b (both inclusive).
from random import randint
print(randint(1, 6)) # simulating a dice roll
Example Output:
3
Repeated Random Rolls
for _ in range(5):
print(randint(1, 6), end=' ')
Output:
5 2 6 1 3
Advanced Random Functions
You can import multiple functions at once:
from random import uniform, randrange, choice, sample, seed, gauss
uniform(a, b)
Returns a float between a and b.
print(uniform(2.222, 2.223))
Output:
2.22245685
randrange(start, stop, step)
Similar to range() but returns one random integer.
print(randrange(100, 201, 2))
print(randrange(1, 49 + 1))
choice(sequence)
Picks one random element from a list, tuple, or string.
print(choice([3, 6, 12, 18, 23, 42]))
print(choice(['ABC', 'DEF', 'GHI']))
print(choice('ABCDEFG'))
sample(sequence, k)
Returns a list of k unique elements.
print(sample('ABCDEFGKLMNOPRS', 3))
Output:
['C', 'R', 'D']
seed()
Sets the starting point for random number generation.
Using the same seed always produces the same sequence — helpful for testing.
from random import seed, randint
seed(42)
print(randint(1, 6)) # 6
print(randint(1, 6)) # 1
print(randint(1, 6)) # 1
Note:
Computers use pseudo-random algorithms — not truly random, but unpredictable enough for most applications.
gauss(mu, sigma)
Generates random numbers following a Gaussian (normal) distribution.
from random import gauss
mean = 100 # expected value
stdev = 50 # standard deviation
print(gauss(mean, stdev))
Example Output:
98.01132723714748
Bonus: Visualizing Random Data with Matplotlib
You can plot Gaussian-distributed data using the Matplotlib library.
# To install matplotlib, run this in your terminal:
# pip install matplotlib
import matplotlib.pyplot as plt
from random import gauss
mean = 100
stdev = 50
values = [gauss(mean, stdev) for _ in range(10_000)]
plt.hist(values, bins=100)
plt.title("Gaussian Distribution Example")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()
Note:
If you get an interpreter or import error, make sure you’ve installed Matplotlib and that your Python environment/interpreter is properly configured (e.g., in PyCharm: check File > Settings > Project Interpreter).

String Formatting (f-Strings)
Introduced in Python 3.6, f-strings are a concise way to embed variables or expressions directly in strings.
first_name = "Peter"
last_name = "Wellert"
output = f"My name is {first_name} {last_name}!"
print(output)
Output:
My name is Peter Wellert!
String Concatenation (Old Way)
output2 = "My name is " + first_name + " " + last_name + "!"
print(output2)
Expressions Inside f-Strings
You can include calculations directly inside {}:
a = 7
b = 8
print(f"The product is: {a * b}")
Output:
The product is: 56
Number Formatting
Format floats with a specific number of decimal places:
value = 1 / 6
print(f"Euro: {value:.2f}")
Output:
Euro: 0.17
Add thousands separators:
big_number = 12345.6789
print(f"{big_number:,.2f}")
Output:
12,345.68
Conditional Expressions in f-Strings
name = "Alex"
age = 20
print(f"{name} is {'an adult' if age >= 18 else 'a minor'}.")
Output:
Alex is an adult.
Input and Output
Output with print()
You can print multiple values separated by commas.
print(1, 2, "Three", 4)
Output:
1 2 Three 4
Custom Separators and End Characters
sep controls how printed items are separated.end defines what is printed at the end (default is \n).
print(1, 2, 3, 4, sep="-")
print(1, 2, 3, 4, sep=", ")
print(1, end="---")
print(2)
Output:
1-2-3-4
1, 2, 3, 4
1---2
Line Breaks with \n
print("Hello\nWorld!")
Output:
Hello
World!
User Input with input()
input() pauses the program and waits for the user’s response.
name = input("Please enter your name: ")
print("Hello,", name)
All inputs are strings by default:
print(type(name)) # <class 'str'>
Convert to numbers if needed:
age = int(input("Enter your age: "))
print(f"Next year, you’ll be {age + 1}.")
Summary
In this lesson, we covered:
- Random number functions:
random(),randint(),uniform(),choice(),sample(),seed(),gauss() - String formatting with f-strings and advanced numeric formats
- Input/output operations (
print(),input(),sep,end) - Visualizing random data using Matplotlib
Next up: Type conversion, lists, and basic data structures in Python!
