Python Articles

Page 797 of 852

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 414 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 460 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 901 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

Where to put comments in an if...elif..else construct?

karthikeya Boyini
karthikeya Boyini
Updated on 17-Jun-2020 1K+ Views

You can put comments anywhere in an if...elif...else statement, ie before each of these blocks or within each of these blocks. Note that you cannot put multiline comments before elif and else blocks though, as these comments are actually strings which imply a break in the whole construct. For example,# If check if True:    # Another Comment style    print("If block") # Else if statement elif False:    # Another Comment style    print("elif block") # Else else:    # Another Comment style    print("Else block")This will give the output:If block

Read More

How to comment each condition in a multi-line if statement in Python?

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

You can do this directly if you are surrounding your multiline if statements conditions in a parenthesis. For example,if (cond1 == 'val1' and    cond2 == 'val2' and # Some comment    cond3 == 'val3' and # Some comment    cond4 == 'val4'):However, this is not possible if you try to do this without a parenthesis. For example, the following code will give an error:if cond1 == 'val1' and \    cond2 == 'val2' and \ # Some comment    cond3 == 'val3' and \ # Some comment    cond4 == 'val4':

Read More
Showing 7961–7970 of 8,519 articles
« Prev 1 795 796 797 798 799 852 Next »
Advertisements