Unit III - Python
Unit III - Python
Unit III - Python
Example:
mylist = "Hello"
x = len(mylist)
cars.pop(1)
You can also use the remove() method to remove an element from the array.
cars.remove("Volvo")
TensorFlow.
Scikit-Learn.
Numpy.
Keras.
PyTorch.
LightGBM.
Eli5.
SciPy.
Example
import math
pie = math.pi
print("The value of pi is : ",pie)
An array in Python is used to store multiple values or items or elements of the same
type in a single variable. To access elements of an array using the index operator [].
To access a particular element is to call the array you created. Beside the array is the
index [] operator, which will have the value of the particular element’s index position
from a given array.
Syntax:
def fun():
statements
.
.
return [expression]
Example:
def my_function(x):
return 5 * x
print(my_function(3))
print(my_function(5))
print(my_function(9))
17. Write a Python function that takes two lists and returns True if they have at least one
common member.
Python also accepts function recursion, which means a defined function can call
itself.Recursion is a common mathematical and programming concept. It means that a
function calls itself. This has the benefit of meaning that you can loop through data to
reach a result.The developer should be very careful with recursion as it can be quite
easy to slip into writing a function which never terminates, or one that uses excess
amounts of memory or processor power. However, when written correctly recursion
can be a very efficient and mathematically-elegant approach to programming.
Example:
def tri_recursion(k):
if(k>0):
result = k+tri_recursion(k-1)
print(result)
else:
result = 0
return result
The below functions are used to change the case of the strings.
lower(): Converts all uppercase characters in a string into lowercase
upper(): Converts all lowercase characters in a string into uppercase
title(): Convert string to title case
Syntax
string_name.capitalize()
Parameter: The capitalize() function does not takes any parameter.
Return: The capitalize() function returns a string with the first character in the
capital.
Example:
name = "Welcome"
print(name.capitalize())
>>>
t= maketable("aeiou","xxxxx")
>>>
phrase= "now is the time for all good men to come to the aid of their party"
>>>
phrase.translate(t)
'nxw xs thx txmx fxr xll gxxd mxn tx cxmx tx thx xxd xf thxxr pxrty'
This module contains a number of definitions of the characters in the ASCII character
set. These definitions serve as a central, formal repository for facts about the character
set. Note that there are general definitions, applicable to Unicode character setts,
different from the ASCII definitions.
ascii_letters
The set of all letters, essentially a union
of ascii_lowercase and ascii_uppercase.
ascii_lowercase
The lowercase letters in the ASCII character
set: 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase
The uppercase letters in the ASCII character
set: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
digits
The digits used to make decimal numbers: '0123456789'
hexdigits
The digits used to make hexadecimal numbers: '0123456789abcdefABCDEF'
letters
This is the set of all letters, a union of lowercase and uppercase, which depends
on the setting of the locale on your system.
lowercase
This is the set of lowercase letters, and depends on the setting of the locale on
your system.
octdigits
The digits used to make octal numbers: '01234567'
printable
All printable characters in the character set. This is a union of digits, letters,
punctuation and whitespace.
punctuation
All punctuation in the ASCII character set, this is '!"#$%&\'()*+,-./:;<=>?
@[\\]^_`{|}~'
uppercase
This is the set of uppercase letters, and depends on the setting of the locale on
your system.
whitespace
A collection of characters that cause spacing to happen. For ASCII this is '\t\n\
x0b\x0c\r ’
b.append(4.4)
print ("Array after insertion : ", end =" ")
for i in (b):
print (i, end =" ")
print()
Example
fruits = ['apple', 'banana', 'cherry', 'orange']
fruits.clear()
3. copy()
The copy() method returns a copy of the specified list.
Syntax
list.copy()
Example
fruits = ['apple', 'banana', 'cherry', 'orange']
x = fruits.copy()
4. count()
The count() method returns the number of elements with the specified value.
Syntax
list.count(value)
Example
fruits = ['apple', 'banana', 'cherry']
x = fruits.count("cherry")
5. extend()
The extend() method adds the specified list elements (or any iterable) to the end of the
current list.
Syntax
list.extend(iterable)
Example
fruits = ['apple', 'banana', 'cherry']
cars = ['Ford', 'BMW', 'Volvo']
fruits.extend(cars)
Python also accepts function recursion, which means a defined function can call itself.
Advantages
Disadvantages
A lot of memory and time is taken through recursive calls which makes it
expensive for use.
Recursive functions are challenging to debug.
The reasoning behind recursion can sometimes be tough to think through.
Syntax:
Example:
def recursive_fibonacci(n):
if n <= 1:
return n
else:
return(recursive_fibonacci(n-1) + recursive_fibonacci(n-2))
n_terms = 10
# check if the number of terms is valid
if n_terms <= 0:
print("Invalid input ! Please input a positive value")
else:
print("Fibonacci series:")
for i in range(n_terms):
print(recursive_fibonacci(i))
The below functions are used to change the case of the strings.
lower(): Converts all uppercase characters in a string into lowercase
upper(): Converts all lowercase characters in a string into uppercase
title(): Convert string to title case
Syntax
string_name.capitalize()
Parameter: The capitalize() function does not takes any parameter.
Return: The capitalize() function returns a string with the first character in the
capital.
Example:
name = "Welcome"
print(name.capitalize())
String casefold() Method
Python String casefold() method is used to convert string to lower case. It is similar
to lower() string method, but case removes all the case distinctions present in a
string.
Syntax:
string.casefold()
Parameters: The casefold() method doesn’t take any parameters.
Return value: Returns the case folded string the string converted to lower case.
Example
string = "Welcome"
print("lowercase string: ", string.casefold())
Capitalizes a string
>>> s = "heLLo BuDdY"
>>> s2 = s.capitalize()
>>> print(s2)
2. s.lower() in Python
import array as arr
a = arr.array('i', [2, 4, 6, 8])
print("First element:", a[0])
print("Second element:", a[1])
print("Second last element:", a[-1])
To delete elements from an array
The elements can be deleted from an array using Python's del statement. If we want to
delete any value from the array, we can do that by using the indices of a particular
element.
import array as arr
number = arr.array('i', [1, 2, 3, 3, 4])
del number[2] # removing third element
print(number) # Output: array('i', [1, 2, 3, 4])
#Initialize array
arr = [1, 2, 3, 4, 5];
print("Original array: ");
for i in range(0, len(arr)):
print(arr[i]),
print("Array in reverse order: ");
#Loop through the array in reverse order
for i in range(len(arr)-1, -1, -1):
print(arr[i]),
Example
def solve(l):
rev = list(reversed(l))
print (rev)
srt = sorted(l)
print(srt)
l = [2,5,8,6,3,4,7,9]
solve(l)
Input
[2,5,8,6,3,4,7,9]
Output:
[9, 7, 4, 3, 6, 8, 5, 2] [2, 3, 4, 5, 6, 7, 8, 9]