Category Archives: Python 101

How to generate random numbers in Python

Let’s say you need to generate random numbers in Python. I need it once for an application that generates random numbers in order to create math problems to be solved by students. But being able to generate random numbers is useful in many other situations. In order to generate random numbers in Python we need to import the random module.

If you just need a random number between 0.0 and 1.0 then just call random.random().

>>> import random
>>> random.random()
0.74002357942266106
>>> random.random()
0.07003308263110275
>>> random.random()
0.14104273470877104

Continue reading

How to convert floating point numbers to integers in Python

Let’s consider you want to convert a floating point number into an integer. There are two ways of doing that.

Method 1 – Truncating

You can truncate the floating point number to just the portion before the decimal point. You can do that with the int() function which truncates the number towards zero.

>>> fpn = 57.83
>>> int(fpn)
57

Continue reading

How to iterate over all of the characters of a string object in Python

Let’s say you need to iterate over of all the characters of a string object and modify every character in some way. You can do it using the for loop.

>>> s1 = 'Dan Dumitrache'
>>> for i in s1:
...     print(i, ' ; ')
...
D  ;
a  ;
n  ;
   ;
D  ;
u  ;
m  ;
i  ;
t  ;
r  ;
a  ;
c  ;
h  ;
e  ;

Continue reading

How to reverse the content of a string using slicing in Python

Let’s say you want to revers the content of a string in Python. For example instead of “Dan” you want to be displayed “naD” (in reverse).

You can do it by using slice notation. You select all of the individual characters but in reverse order.

>>> s1 = 'Dan'
>>> s2 = s1[::-1]
>>> s2
'naD'

Continue reading

How to replace a substring in a string object in Python

Let’s consider that you need to replace a substring with a new substring within a string object.

>>> s1 = 'Dan is 39 years old. 39 is a nice age although other people think that you are old when you are 39 yea
>>> s2 = s1.replace('39', '40')
>>> s2
'Dan is 40 years old. 40 is a nice age although other people think that you are old when you are 40 years old.'

Continue reading

How to use the polymorphic operator “in”, the “find()”, and “count()” methods to search for a substring in Python

Let’s say that you want to search for a substring within a string object. Nothing easier. In Python, there is a polymorphic operator named in which allows you to do just that! Let’s try it!

>>> s1 = 'Dan is a web developer based within the EU.'
>>> 'Dan' in s1
True
>>> 'Dumitrache' in s1
False
>>> 'EU' in s1
True
>>> 'Canada' in s1
False

Continue reading

How to compare strings in Python

Let’s say you want to compare two string to see if they have the same value. You can do it using the comparison operator is equal to which is represented by == (two equal signs).

>>> s1 = 'Dan'
>>> s2 = 'Dumitrache'
>>> if s1 == s2:
...     print('Strings are equal!')
... else:
...     print('Strings are not equal!')
... 
Strings are not equal!

Continue reading

How to add a string to a number in Python

Let’s say you want to add a string and a number together. For example you have the string “The number of monkeys = ” and the number 17. You would like to add them together and display “The number of monkeys = 17”.

Let’s try this into the terminal:

>>> new_string = 'Number of monkeys = ' + 17
Traceback (most recent call last):
  File "", line 1, in 
TypeError: Can't convert 'int' object to str implicitly

Continue reading