Python Articles

Page 756 of 852

How to check if a float value is a whole number in Python?

George John
George John
Updated on 17-Jun-2020 2K+ Views

To check if a float value is a whole number, use the float.is_integer() method. For example,print((10.0).is_integer()) print((15.23).is_integer())This will give the outputTrue False

Read More

How to compare string and number in Python?

Nikitha N
Nikitha N
Updated on 17-Jun-2020 3K+ Views

Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address. When you order two strings or two numeric types the ordering is done in the expected way (lexicographic ordering for string, numeric ordering for integers).When you order a numeric and a non-numeric type, the numeric type comes first.If you have a number in a str object, you can simply convert it to a float or an int using their respective constructors. For example, i = 100 j = "12" int_j = int(j) print(int_j ...

Read More

How to Calculate the Area of a Triangle using Python?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 17-Jun-2020 1K+ Views

Calculating the area of a triangle is a formula that you can easily implement in python. If you have the base and height of the triangle, you can use the following code to get the area of the triangle,def get_area(base, height):    return 0.5 * base * height print(get_area(10, 15))This will give the output:75If you have the sides of the triangle, you can use herons formula to get the area. For example,def get_area(a, b, c):    s = (a+b+c)/2    return (s*(s-a)*(s-b)*(s-c)) ** 0.5 print(get_area(10, 15, 10))This will give the output:49.607837082461074

Read More

How to use multiple for and while loops together in Python?

Ankith Reddy
Ankith Reddy
Updated on 17-Jun-2020 413 Views

You can create nested loops in python fairly easily. You can even nest a for loop inside a while loop or the other way around. For example,for i in range(5):    j = i    while j != 0:       print(j, end=', ')       j -= 1    print("")This will give the output1, 2, 1, 3, 2, 1, 4, 3, 2, 1,You can take this nesting to as many levels as you like.

Read More

Can we change Python for loop range (higher limit) at runtime?

Samual Sam
Samual Sam
Updated on 17-Jun-2020 459 Views

No, You can't modify a range once it is created. Instead what you can do is use a while loop instead. For example, if you have some code like:for i in range(lower_limit, higher_limit, step_size):# some code if i == 10:    higher_limit = higher_limit + 5You can change it to:i = lower_limit while i < higher_limit:    # some code    if i == 10:       higher_limit = higher_limit + 5    i += step_size

Read More

How to create a triangle using Python for loop?

Sravani S
Sravani S
Updated on 17-Jun-2020 6K+ Views

There are multiple variations of generating triangle using numbers in Python. Let's look at the 2 simplest forms:for i in range(5):    for j in range(i + 1):       print(j + 1, end="")    print("")This will give the output:1 12 123 1234 12345You can also print numbers continuously using:start = 1 for i in range(5):    for j in range(i + 1):       print(start, end=" ")       start += 1    print("")This will give the output:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15You can also print these numbers in reverse using:start = 15 for i in range(5):    for j in range(i + 1):       print(start, end=" ")       start -= 1    print("")This will give the output:15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

Read More

How do I run two python loops concurrently?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 17-Jun-2020 899 Views

You will need to use a multiprocessing library. You will need to spawn a new process and provide the code to it as an argument. For example,from multiprocessing import Processdef loop_a():    for i in range(5):       print("a") def loop_b():    for i in range(5):       print("b") Process(target=loop_a).start() Process(target=loop_b).start()This might process different outputs at different times. This is because we don't know which print will be executed when.

Read More

How to handle exception inside a Python for loop?

Arjun Thakur
Arjun Thakur
Updated on 17-Jun-2020 3K+ Views

You can handle exception inside a Python for loop just like you would in a normal code block. This doesn't cause any issues. For example,for i in range(5):    try:       if i % 2 == 0:          raise ValueError("some error")       print(i) except ValueError as e:    print(e)This will give the outputsome error 1 some error 3 some error

Read More

How to write inline if statement for print in Python?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 17-Jun-2020 7K+ Views

Python provides two ways to write inline if statements. These are:1. if condition: statement2. s1 if condition else s2Note that second type of if cannot be used without an else. Now you can use these inline in a print statement as well. For example,a = True if a: print("Hello")This will give the output:Helloa = False print("True" if a else "False")This will give the output:False

Read More

How to use if...else statement at the command line in Python?

Priya Pallavi
Priya Pallavi
Updated on 17-Jun-2020 2K+ Views

There are multiple ways in which you can use if else construct in the command line in python. For example, bash supports multiline statements, which you can use like:$ python -c ' > a = True > if a: > print("a is true") > 'This will give the output:a is trueIf you prefer to have the python statement in a single line, you can use the newline between the commands. For example,$ python -c $'a = Trueif a: print("a is true");'This will give the output:a is true

Read More
Showing 7551–7560 of 8,519 articles
« Prev 1 754 755 756 757 758 852 Next »
Advertisements