Python Ternary Conditional Operator

This article shows you how to write Python ternary operator, also called conditional expressions. <expression1> if <condition> else <expression2> expression1 will be evaluated if the condition is true, otherwise expression2 will be evaluated. 1. Ternary Operator 1.1 This example will print whether a number is odd or even. n = 5 print("Even") if n % …

Read more

Python – Get the last element of a list

In Python, we can use index -1 to get the last element of a list. #!/usr/bin/python nums = [1, 2, 3, 4, 5] print(nums[-1]) print(nums[-2]) print(nums[-3]) print(nums[-4]) print(nums[-5]) print(nums[0]) print(nums[1]) print(nums[2]) print(nums[3]) print(nums[4]) Output 5 4 3 2 1 1 2 3 4 5 Yet another example. #!/usr/bin/python # getting list of nums from the …

Read more