Python Programming
Please leave a remark at the bottom of each page with your useful suggestion.
Table of Contents
- Python Introduction
- Python Startup
- Python Examples Program Collection 1
- Python Examples Program Collection 2
- Python Examples Program Collection 3
- Python Examples Program Collection 4
- Python Examples Program Collection 5
- Python Examples Program Collection 6
- Python Examples Program Collection 7
- Python Examples Program Collection 8
Python __import__()
Example: How __import()__ works?
mathematics = __import__('math', globals(), locals(), [], 0)
print(mathematics.fabs(-2.5))
Python abs()
number = -20
absolute_number = abs(number)
print(absolute_number)
# Output: 20
Python abs()
Example 1: Get absolute value of a number
# random integer
integer = -20
print('Absolute value of -20 is:', abs(integer))
#random floating number
floating = -30.33
print('Absolute value of -30.33 is:', abs(floating))
Python abs()
Example 2: Get magnitude of a complex number
# random complex number
complex = (3 - 4j)
print('Magnitude of 3 - 4j is:', abs(complex))
Python Set add()
prime_numbers = {2, 3, 5, 7}
# add 11 to prime_numbers
prime_numbers.add(11)
print(prime_numbers)
# Output: {2, 3, 5, 7, 11}
Python Set add()
Example 1: Add an element to a set
# set of vowels
vowels = {'a', 'e', 'i', 'u'}
# adding 'o'
vowels.add('o')
print('Vowels are:', vowels)
# adding 'a' again
vowels.add('a')
print('Vowels are:', vowels)
Python Set add()
Example 2: Add tuple to a set
# set of vowels
vowels = {'a', 'e', 'u'}
# a tuple ('i', 'o')
tup = ('i', 'o')
# adding tuple
vowels.add(tup)
print('Vowels are:', vowels)
# adding same tuple again
vowels.add(tup)
print('Vowels are:', vowels)
Python all()
boolean_list = ['True', 'True', 'True']
# check if all elements are true
result = all(boolean_list)
print(result)
# Output: True
Python all()
Example 1: How all() works for lists?
# all values true
l = [1, 3, 4, 5]
print(all(l))
# all values false
l = [0, False]
print(all(l))
# one false value
l = [1, 3, 4, 0]
print(all(l))
# one true value
l = [0, False, 5]
print(all(l))
# empty iterable
l = []
print(all(l))
Python all()
Example 2: How all() works for strings?
s = "This is good"
print(all(s))
# 0 is False
# '0' is True
s = '000'
print(all(s))
s = ''
print(all(s))
Python any()
boolean_list = ['True', 'False', 'True']
# check if any element is true
result = any(boolean_list)
print(result)
# Output: True
Python any()
Example 1: Using any() on Python Lists
# True since 1,3 and 4 (at least one) is true
l = [1, 3, 4, 0]
print(any(l))
# False since both are False
l = [0, False]
print(any(l))
# True since 5 is true
l = [0, False, 5]
print(any(l))
# False since iterable is empty
l = []
print(any(l))
Python any()
Example 2: Using any() on Python Strings
# At east one (in fact all) elements are True
s = "This is good"
print(any(s))
# 0 is False
# '0' is True since its a string character
s = '000'
print(any(s))
# False since empty iterable
s = ''
print(any(s))
Python List append()
currencies = ['Dollar', 'Euro', 'Pound']
# append 'Yen' to the list
currencies.append('Yen')
print(currencies)
# Output: ['Dollar', 'Euro', 'Pound', 'Yen']
Python List append()
Example 1: Adding Element to a List
# animals list
animals = ['cat', 'dog', 'rabbit']
# Add 'guinea pig' to the list
animals.append('guinea pig')
print('Updated animals list: ', animals)
Python List append()
Example 2: Adding List to a List
# animals list
animals = ['cat', 'dog', 'rabbit']
# list of wild animals
wild_animals = ['tiger', 'fox']
# appending wild_animals list to animals
animals.append(wild_animals)
print('Updated animals list: ', animals)
Python ascii()
text = 'Pythön is interesting'
# replace ö with its ascii value
print(ascii(text))
# Output: 'Pyth\xf6n is interesting'
Python ascii()
Example 1: Python ascii()
text1 = '√ represents square root'
# replace √ with ascii value
print(ascii(text1))
text2 = 'Thör is coming'
# replace ö with ascii value
print(ascii(text2))
Python ascii()
Example 2: ascii() with a List
list = ['Python', 'öñ', 5]
# ascii() with a list
print(ascii(list))
Python ascii()
Example 3: ascii() with a Set
set = {'Π', 'Φ', 'η'}
// ascii() with a set
print(ascii(set))
Python ascii()
Example 4 : ascii() with a Tuple
tuple = ('ö', '√', '¶','Ð','ß' )
// ascii() with a tuple
print(ascii(tuple))
Python bin()
number = 15
# convert 15 to its binary equivalent
print('The binary equivalent of 15 is', bin(number))
# Output: The binary equivalent of 15 is 0b1111
Python bin()
Example 1: Python bin()
number = 5
# convert 5 to its binary equivalent
print('The binary equivalent of 5 is:', bin(number))
Python bin()
Example 2: Python bin() with a Non-Integer Class
class Quantity:
apple = 1
orange = 2
grapes = 2
def func():
return apple + orange + grapes
print('The binary equivalent of quantity is:', bin(Quantity()))
Python bin()
Example 3: bin() with __index__() for Non-Integer Class
class Quantity:
apple = 1
orange = 2
grapes = 2
def __index__(self):
return self.apple + self.orange + self.grapes
print('The binary equivalent of quantity is:', bin(Quantity()))
Python bool()
Example-
test = 1
# returns boolean value of 1
print(test, 'is', bool(test))
# Output: 1 is True
Python bool()
Example 1: Python bool() with True Arguments
test = 254
# bool() with an integer number
print(test, 'is', bool(test))
test1 = 25.14
# bool() with a floating point number
print(test1, 'is', bool(test1))
test2 = 'Python is the best'
# bool() with a string
print(test2, 'is', bool(test2))
test3 = True
# bool() with True
print(test3, 'is', bool(test3))
Python bool()
Example 2: bool() with False Arguments
test = []
# bool() with an empty argument
print(test, 'is' ,bool(test))
test1 = 0
# bool() with zero
print(test1, 'is' ,bool(test1))
test2 = None
# bool() with none
print(test2, 'is' ,bool(test2))
test3 = False
# bool() with False
print(test3, 'is' ,bool(test3))
Python bytearray()
prime_numbers = [2, 3, 5, 7]
# convert list to bytearray
byte_array = bytearray(prime_numbers)
print(byte_array)
# Output: bytearray(b'\x02\x03\x05\x07')
Python bytearray()
Example 1: Array of bytes from a string
string = "Python is interesting."
# string with encoding 'utf-8'
arr = bytearray(string, 'utf-8')
print(arr)
Python bytearray()
Example 2: Array of bytes of given integer size
size = 5
arr = bytearray(size)
print(arr)
Python bytearray()
Example 3: Array of bytes from an iterable list
rList = [1, 2, 3, 4, 5]
arr = bytearray(rList)
print(arr)
Python bytes()
message = 'Python is fun'
# convert string to bytes
byte_message = bytes(message, 'utf-8')
print(byte_message)
# Output: b'Python is fun'
Python bytes()
Example 1: Convert string to bytes
string = "Python is interesting."
# string with encoding 'utf-8'
arr = bytes(string, 'utf-8')
print(arr)
Python bytes()
Example 2: Create a byte of given integer size
size = 5
arr = bytes(size)
print(arr)
Python bytes()
Example 3: Convert iterable list to bytes
rList = [1, 2, 3, 4, 5]
arr = bytes(rList)
print(arr)
Python callable()
Example 1: How callable() works?
x = 5
print(callable(x))
def testFunction():
print("Test")
y = testFunction
print(callable(y))
Python callable()
Example 2: Callable Object
class Foo:
def __call__(self):
print('Print Something')
print(callable(Foo))
Python callable()
Example 3: Object Appears to be Callable but isn't callable.
class Foo:
def printLine(self):
print('Print Something')
print(callable(Foo))