You are on page 1of 37

Short Review for SAU 1

Programming
Themes
• Generate pseudo-random numbers and sequences;
• Creating images;
• Image processing;
• Sound processing;
RANDOM
Random module
The random module provides functions for generating random numbers, letters, and
random selection of sequence elements (list, string, etc).

To use these functions at the beginning of the program, you need to connect the
module, which is done by the import command:
import random
After connecting the module, we can use its functions.
All Random functions
1. randint()
2. randrange()
3. random()
4. uniform()
5. shuffle()
6. choice()
7. sample()
1. randint()
Template: Function returns the random integer value between a
random.randint(a,b) and b inclusive.

Example:
a – integer number *
num1 = random.randint(0,17)
b – integer number * print(num1)

Output:
5
2. randrange()
Function returns a randomly selected element from the
Template: specified range.

random.randrange(a,b,c) Example 1:
num1 = random.randrange(17)
Num2 = random.randrange(10,121,5)
a – start point (incl.)
print(num1)
b – end point (not incl.) * print(num2)
c – steps
Output:
11
25
3. uniform()
Template: Function returns the random float value between a
random.uniform(a,b) and b.

Example:
a – float number *
num1 = random.uniform(1.5,3,5)
b – float number * print(num1)

Output:
4.436944547522564
4. random()
Template: Function returns the random float value below 1.
random.random()
Example:
num1 = random.random()
no parameters needed.
print(num1)

Output:
0.5469797261645722
5. shuffle()
Template: Function returns the values within the set of data in
randomized order.
random.shuffle(list)

Example:
list – randomizing set of numbers = [4,9,8,7,2,6,5,1,5,8]
data * num1 = random.shuffle(numbers)
print(numbers)

Output:
[5,1,8,5,1,9,4,7,2,8]
6. choice()
Template: Function returns the random value between the elements of the given data
set.
random.choice(A)
Example:
print(random.choice(‘YAOURT’))

A – lists, sets, tulips, words print(random.choice([5,8,9,1,2,3,7]))

*: print(random.choice([‘a’,’n’,’f’,’s’]))

(‘PYTHON’)
Output:
([1,2,3,4,5,6,7])
R
([‘a’,’b’,’c’,’d’]) 7
f
7. sample()
Template: Function returns the a amount of elements from the
random.sample(list,a) data set ‘list’.

Example:
list – name of the set of
numbers = [1,1,4,1,9,6]
the data *
print(random.sample(numbers,3))
a – integer number (amount
of elements) *
Output:
[6,1,9]
TKINTER
Tk (Tkinter) module
The Tk (Tkinter) module provides functions for generating random numbers, letters,
and random selection of sequence elements (list, string, etc).

To use these functions at the beginning of the program, you need to connect the
module, which is done by the import command:
import tkinter
After connecting the module, we can use its functions.
All GUI Tkinter functions
1. Tk() 1. randint() 1. randint()
2. randrange() 2. randrange() 2. randrange()
3. random() 3. random() 3. random()
4. uniform() 4. uniform() 4. uniform()
5. shuffle() 5. shuffle() 5. shuffle()
6. choice() 6. choice() 6. choice()
7. sample() 7. sample() 7. sample()
1. Tk()
Tk() – used to create main window.

Example:
num1 = random.randint(0,17)
print(num1)

Output:
5
RANDOM
Random module
The random module provides functions for generating random numbers, letters, and
random selection of sequence elements (list, string, etc).

To use these functions at the beginning of the program, you need to connect the
module, which is done by the import command:
import random
After connecting the module, we can use its functions.
All Random functions
1. randint()
2. randrange()
3. random()
4. uniform()
5. shuffle()
6. choice()
7. sample()
1. randint()
Template: Function returns the random integer value between a
random.randint(a,b) and b inclusive.

Example:
a – integer number *
num1 = random.randint(0,17)
b – integer number * print(num1)

Output:
5
2. randrange()
Function returns a randomly selected element from the
Template: specified range.

random.randrange(a,b,c) Example 1:
num1 = random.randrange(17)
Num2 = random.randrange(10,121,5)
a – start point (incl.)
print(num1)
b – end point (not incl.) * print(num2)
c – steps
Output:
11
25
3. uniform()
Template: Function returns the random float value between a
random.uniform(a,b) and b.

Example:
a – float number *
num1 = random.uniform(1.5,3,5)
b – float number * print(num1)

Output:
4.436944547522564
4. random()
Template: Function returns the random float value below 1.
random.random()
Example:
num1 = random.random()
no parameters needed.
print(num1)

Output:
0.5469797261645722
5. shuffle()
Template: Function returns the values within the set of data in
randomized order.
random.shuffle(list)

Example:
list – randomizing set of numbers = [4,9,8,7,2,6,5,1,5,8]
data * num1 = random.shuffle(numbers)
print(numbers)

Output:
[5,1,8,5,1,9,4,7,2,8]
6. choice()
Template: Function returns the random value between the elements of the given data
set.
random.choice(A)
Example:
print(random.choice(‘YAOURT’))

A – lists, sets, tulips, words print(random.choice([5,8,9,1,2,3,7]))

*: print(random.choice([‘a’,’n’,’f’,’s’]))

(‘PYTHON’)
Output:
([1,2,3,4,5,6,7])
R
([‘a’,’b’,’c’,’d’]) 7
f
7. sample()
Template: Function returns the a amount of elements from the
random.sample(list,a) data set ‘list’.

Example:
list – name of the set of
numbers = [1,1,4,1,9,6]
the data *
print(random.sample(numbers,3))
a – integer number (amount
of elements) *
Output:
[6,1,9]
RANDOM
Random module
The random module provides functions for generating random numbers, letters, and
random selection of sequence elements (list, string, etc).

To use these functions at the beginning of the program, you need to connect the
module, which is done by the import command:
import random
After connecting the module, we can use its functions.
All Random functions
1. randint()
2. randrange()
3. random()
4. uniform()
5. shuffle()
6. choice()
7. sample()
1. randint()
Template: Function returns the random integer value between a
random.randint(a,b) and b inclusive.

Example:
a – integer number *
num1 = random.randint(0,17)
b – integer number * print(num1)

Output:
5
2. randrange()
Function returns a randomly selected element from the
Template: specified range.

random.randrange(a,b,c) Example 1:
num1 = random.randrange(17)
Num2 = random.randrange(10,121,5)
a – start point (incl.)
print(num1)
b – end point (not incl.) * print(num2)
c – steps
Output:
11
25
3. uniform()
Template: Function returns the random float value between a
random.uniform(a,b) and b.

Example:
a – float number *
num1 = random.uniform(1.5,3,5)
b – float number * print(num1)

Output:
4.436944547522564
4. random()
Template: Function returns the random float value below 1.
random.random()
Example:
num1 = random.random()
no parameters needed.
print(num1)

Output:
0.5469797261645722
5. shuffle()
Template: Function returns the values within the set of data in
randomized order.
random.shuffle(list)

Example:
list – randomizing set of numbers = [4,9,8,7,2,6,5,1,5,8]
data * num1 = random.shuffle(numbers)
print(numbers)

Output:
[5,1,8,5,1,9,4,7,2,8]
6. choice()
Template: Function returns the random value between the elements of the given data
set.
random.choice(A)
Example:
print(random.choice(‘YAOURT’))

A – lists, sets, tulips, words print(random.choice([5,8,9,1,2,3,7]))

*: print(random.choice([‘a’,’n’,’f’,’s’]))

(‘PYTHON’)
Output:
([1,2,3,4,5,6,7])
R
([‘a’,’b’,’c’,’d’]) 7
f
7. sample()
Template: Function returns the a amount of elements from the
random.sample(list,a) data set ‘list’.

Example:
list – name of the set of
numbers = [1,1,4,1,9,6]
the data *
print(random.sample(numbers,3))
a – integer number (amount
of elements) *
Output:
[6,1,9]

You might also like