Python Code Examples: Practice is the key to become a better programmer. As we know that Python is one of the top Programming Languages to learn.

So, today we will discuss variety of python code with there complete explanation.

These output based programs will help you in brushing up your Python Programming concepts.

So, let see some the Python Code with their output and complete explanation:

1. Python Code Example of range() Function

data = 0

for i in range(10,2,-2):
data += i


Output of above Program:

Explanation of above Program:
In python, range() is a built-in function, which is used to return a sequence of numbers given between the starting and ending argument of the function.

We can also control the amount of steps it should take in a particular direction.

As in the above code, the first and second argument we provided is 10 and 2. So, we have to take the sequence of numbers between 10 to 2.

In the third argument, we have provided -2, which means we have to decrement 2 from the first argument(10) till it reaches the second argument(2).

2. Python Code Example of Constructor

class Company:
    def init(self, salary):
    self.salary = salary
    salary = 300

    company = Company(200)
    print (company.salary)

Output of above Program:

Explanation of above Program:
Company is a class in which _init_ method is automatically called. As it is a constructor of the class.

Here, self is the object of the class Company and the salary argument is the data attribute of the self object.

Then we assigned 200 to the data attribute of the class, which is self.salary.

As a result, when we print company.salary, the data attribute of the class which is 200 got printed.

3. Python Code Example of or, and Operator

x = True
y = False
z = False

if x or y and z:
    print ("CODERPEDIA")
    print ("coderpedia")

Output of above Program:

Explanation of above Program:
In programming, “and” operator have the more priority as compared to “or” operator.

Therefore, In this program, firstly we resolved “y and z” which gives us False and then we resolved “x or False”, which give us true. As the output is CODERPEDIA.

4. Code Example of List


print (List1)

print (List2)

print (List2)

Output of above Program:
[4, 5, 6]
[4, 5, 6, 7, 8]
[4, 5, 6, 7, 8, 4, 5, 6]

Explanation of above Program:
In python, list is a collection of similar or different types of data.

By using the index values in list, We can select the specific number of elements in sequence.

So, the first index value is the starting index and the second index value is the ending between which we can select the values.

In list, extend function is used to combine the two list. As a result, in the third line of the output, we get the combined list of list1 and list2.

5. Code Example of lambda

apple = lambda s: s * 3
banana = lambda s: s * 4

price = 2

price = apple(price)
price = banana(price)
price = apple(price)

print (price)

Output of above Program:

Explanation of above Program:
In Python, lambda is a anonymous function, which can take any number of arguments but return only single expression.

Like – In this example we have used ‘s’ argument and returned ‘s * 3’ expression in the lambda function.

So, Initially the value of price is 2. We passed price to apple function and returned the value 6.

Similarly, we passes the value of price which is 6, to the banana function, which returned 24.

At last we passed, updated value of price to the apple function again and it returned 72. Therefore, the value of price is 72.

So, these are some of the Python Code Examples with their complete explanation.