Wednesday, June 16, 2021

Random Number Functions



Sr.No.

Function & Description

1

choice(seq)

A random item from a list, tuple, or string.

2

randrange ([start,] stop [,step])

A randomly selected element from range(start, stop, step).

3

random()

A random float r, such that 0 is less than or equal to r and r is less than 1

4

seed([x])

Sets the integer starting value used in generating random numbers. Call this function before calling any other random module function. Returns None.

5

shuffle(lst)

Randomizes the items of a list in place. Returns None.

6

uniform(x, y)

A random float r, such that x is less than or equal to r and r is less than y.


Updating Strings

You can "update" an existing string by (re)assigning a variable to another string. The new value can be related to its previous value or to a completely different string altogether. For example −



#!/usr/bin/python3


var1 = 'Hello World!'

print ("Updated String :- ", var1[:6] + 'Python')



String Special Operators

Assume string variable a holds 'Hello' and variable b holds 'Python', then −



Operator

Description

Example

+

Concatenation - Adds values on either side of the operator

a + b will give HelloPython

*

Repetition - Creates new strings, concatenating multiple copies of the same string

a*2 will give -HelloHello

[]

Slice - Gives the character from the given index

a[1] will give e

[ : ]

Range Slice - Gives the characters from the given range

a[1:4] will give ell

in

Membership - Returns true if a character exists in the given string

H in a will give 1

not in

Membership - Returns true if a character does not exist in the given string

M not in a will give 1

r/R

Raw String - Suppresses actual meaning of Escape characters. The syntax for raw strings is exactly the same as for normal strings with the exception of the raw string operator, the letter "r," which precedes the quotation marks. The "r" can be lowercase (r) or uppercase (R) and must be placed immediately preceding the first quote mark.

print r'\n' prints \n and print R'\n'prints \n


Basic List Operations

Lists respond to the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new list, not a string.

In fact, lists respond to all of the general sequence operations we used on strings in the prior chapter.

Python Expression

Results

Description

len([1, 2, 3])

3

Length

[1, 2, 3] + [4, 5, 6]

[1, 2, 3, 4, 5, 6]

Concatenation

['Hi!'] * 4

['Hi!', 'Hi!', 'Hi!', 'Hi!']

Repetition

3 in [1, 2, 3]

True

Membership

for x in [1,2,3] : print (x,end = ' ')

1 2 3

Iteration



Indexing, Slicing and Matrixes

Since lists are sequences, indexing and slicing work the same way for lists as they do for strings.

Assuming the following input −

L = ['C++'', 'Java', 'Python']


Python Expression

Results

Description

L[2]

'Python'

Offsets start at zero

L[-2]

'Java'

Negative: count from the right

L[1:]

['Java', 'Python']

Slicing fetches sections


Delete List Elements

To remove a list element, you can use either the del statement if you know exactly which element(s) you are deleting. You can use the remove() method if you do not know exactly which items to delete. For example −

Live Demo

#!/usr/bin/python3


list = ['physics', 'chemistry', 1997, 2000]

print (list)


del list[2]



Lists respond to the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new list, not a string.

In fact, lists respond to all of the general sequence operations we used on strings in the prior chapter.

Python Expression

Results

Description

len([1, 2, 3])

3

Length

[1, 2, 3] + [4, 5, 6]

[1, 2, 3, 4, 5, 6]

Concatenation

['Hi!'] * 4

['Hi!', 'Hi!', 'Hi!', 'Hi!']

Repetition

3 in [1, 2, 3]

True

Membership

for x in [1,2,3] : print (x,end = ' ')

1 2 3

Iteration



No comments:

Post a Comment