Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Python Articles
Page 756 of 852
How to check if a float value is a whole number in Python?
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 MoreHow to compare string and number in Python?
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 MoreHow to Calculate the Area of a Triangle using Python?
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 MoreHow to use multiple for and while loops together in Python?
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 MoreCan we change Python for loop range (higher limit) at runtime?
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 MoreHow to create a triangle using Python for loop?
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 MoreHow do I run two python loops concurrently?
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 MoreHow to handle exception inside a Python for loop?
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 MoreHow to write inline if statement for print in Python?
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 MoreHow to use if...else statement at the command line in Python?
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