 # How to do Python Programming Practice Effectively?

This post of Python part includes an array of python programming practices. The practice examples are classified depending on the basic examples, involving an array, List, string, searching and sorting, and much more. Every code example includes various ways to resolve coding issues. Let’s check all these codings and practice them regularly.

### What is the python coding language?

Python is one of the generally used general-purpose, high-level,  dynamic, interpreted coding languages. Its study theoretical emphasizes language readability, and its syntax enables learners to formulate theories in some codes’ lines more than reasonable in codings like Java or C++.

Python helps various coding paradigms, involving imperative, object-oriented, and procedural or functional coding styles. It emphasizes a powerful kind system and automated memory management and has a wide and extensive standard library to use.

The most reliable method by which the learners can learn anything is by exercise and practice questions. This section is only for those (a newcomer to intermediate) who are well-versed with Python. These activities encourage the students to enhance their Python programming skills.

## Let’s take some of the python programming practice

Python programming practice to add two numbers

Let’s take two numbers as num1 and num2. The job is to execute a Python code to see the sum of the two numbers.

For Instance:

Input: num1 = 6, num2 = 1

Output: 7

Input: num1 = 12, num2 = 5

Output: 17

In this python programming practice code, we will sum two numbers. The learners can initially be required to put two numbers. The data is considered utilizing the input() function and saved in the variables num1 and num2. Then, the variables num1 and num2 are calculated utilizing the arithmetic operative +, and the output is saved in the variable called Add.

See also  What is Memory leaks in Python And How to Remove Them?

### Here is the Python code to sum two numbers:

Example 1:

num1 = 12

num2 = 15

# Summing two nos

# printing the output values

Output:

The addition of 12 and 15 is 27.

Python code to calculate the sum of array

Supply an array of some integers, calculate the addition of its constituents.

For instance:

Input : arr[] = {2, 4, 6}

Output : 12

2 + 4 + 6 = 12

Input : arr[] = {10, 20, 30, 40}

Output : 100

### Technique 1:

# Python program to calculate addition of constituents in the given array

def _sum(arr,n):

# return sum using sum

# inbuilt sum() function

return(sum(arr))

# driver function

arr=[]

# input variables to the list

arr = [10, 20, 30, 40]

# measure the length of the array

n = len(arr)

ans = _sum(arr,n)

# display sum

print (‘The addition of the given array is ‘, ans)

Output:

The addition of the given array is 100.

### Technique 2:

# Python programming practice to calculate sum

# of constituents in an array

# driver function

arr = []

# input variables to the list

arr = [10, 20, 30, 40]

# sum() is the inbuilt functions in a python code that sum

# all the variables of the list, set and returns

# the variables

ans = sum(arr)

# display sum

print (‘The addition of the given array is ‘,ans)

Output:

The addition of the given array is 100.

Python code to swap the two components within a list

Presented a program in Python and presented the elements’ positions, drafting a code to swap the two components within the list.

For instance:

Input : List = [10, 20, 30, 40], pos1 = 2, pos2 = 4

Output : [10, 40, 30, 20]

Input : List = [2, 4, 6, 8, 10], pos1 = 4, pos2 = 10

Output : [2, 10, 6, 8, 4]

### Method #1: Simple swap

As the locations of the components are identified, the programmer can swap the locations of the components.

# Python code to swap components

# at a given locations

# Swap function

def swapPositions(list, pos1, pos2):

list[pos1], list[pos2] = list[pos2], list[pos1]

return list

# Driver function

List = [10, 20, 30, 40]

pos1, pos2 = 2, 4

print(swapLocations(List, pos1-1, pos2-1))

Output:

[10, 40, 30, 20]

### Method #2: Utilizing the Inbuilt list.pop() function

Pop the component at pos1 and save it in a single variable. Furthermore, pop the component at pos2 and save it to another value. Now enter the two popped components at one another’s initial location.

# Python code to swap components

# at a given locations

# Swap function

def swapLocation (list, pos1, pos2):

# popping the components from a given list

first_ele = list.pop(pos1)

second_ele = list.pop(pos2-1)

# putting them to each other’s locations

list.insert(pos1, second_ele)

list.insert(pos2, first_ele)

return list

# Driver function

List = [10, 20, 30, 40]

pos1, pos2 = 2, 4

print(swapLocation (List, pos1-1, pos2-1))

Output:

[10, 40, 30, 20]

### Method #3: Utilizing a tuple variable

Put the component at pos1 and pos2 as a couple in a tuple value. Remove those components with pos2 and pos1 positions on the given list. Now, both the locations in a list will be swapped.

# Python code to swap components at

# a given locations

# Swap function

def swapLocation (list, pos1, pos2):

# Storing the two components

# as a couple in a tuple values get

get = list[pos1], list[pos2]

# remove those components

list[pos2], list[pos1] = get

return list

# Driver Code

List = [10, 20, 30, 40]

pos1, pos2 = 2, 4

print(swapLocations(List, pos1-1, pos2-1))

Output:

[10, 40, 30, 20]

### Method #4: Utilizing comma assignment

# Python code to swap components

# at a given location

def swapLocations(list, pos1, pos2):

list[pos1],list[pos2] = list[pos2],list[pos1]

return list

# Driver Code

List = [10, 20, 30, 40]

pos1, pos2 = 2, 4

print(swapLocations(List, pos1-1, pos2-1))

#### Output:

[10, 40, 30, 20]

Python programming practice for reverse words for a string

This post has provided a string, and it requires to be reverse words of the returned string?

For instance:

Input : str = “statanalytica python programming practice program”

Output : str = “program practice programming python statanalytica”

# Function to reverse words of a string

def rev_sentence(sentence):

# initial split a string with the words

words = sentence.split(‘ ‘)

# reverse the split list of string and connect utilizing space

reverse_sentence = ‘ ‘.join(reversed(words))

# now return the joined string

return reverse_sentence

if __name__ == “__main__”:

input = ‘statanalytica python programming practice program’

print rev_sentence(input)

### Output:

“program practice programming python statanalytica”

Python programming practice code for linear search

Question: Provided an array arr[] of n components, execute a function to find a given component x in arr[].

For instance:

Input : arr[] = {5, 10, 15, 20, 25, 30,

35, 40, 45, 50}

x = 30;

Output : 6

Component x is located at index 6

Input : arr[] = {5, 10, 15, 20, 25, 30,

35, 40, 45, 50}

x = 2;

Output : -1

Component x is not located in arr[].

A simplistic method is to perform a linear search, that is:

• Begin from the left component of arr[] and analyze x one by one with each component of arr[].
• When x gets the value with a component, pass the index.
• If x does not meet with any of the components, return -1.

For instance:

# Finding a component in an array/list in python

# might be perform utilizing \’in\’ operator.

# When x in arr:

#   print arr.index(x)

# If one wishes to execute Linear Search within python

# Linearly search x in arr[]

# When x is matched then return its position

# else return -1

def search(arr, x):

for i in range(len(arr)):

if arr[i] == x:

return i

return -1

## Conclusion

In this article, we have included the details about python programming practice. This post has different practice programs that can help beginners to understand the concepts of python programming language. Besides this, the learners are required to practice them regularly so that they can experiment with them accordingly. This can enhance your knowledge as well as conceptualize these codes.

If you have any problem regarding your programming and python assignments and homework, you can ask for our experts’ help. We can provide you high-quality content along with the plagiarism reports. We can also provide instant help to you as we are accessible 24*7. Besides this, we also provide the assignments with well-formatted structures and deliver them within the slotted time. All these facilities are available at a minimal price. Get the best help with python assignment from the experts.