Subscribe Us

LightBlog

Breaking

LightBlog

Friday, August 16, 2024

Python Basics Programs with Explanations

Python Basics Programs with Explanations

Python Basics Programs with Explanations

Welcome to our comprehensive guide on Python basics. This post covers a variety of Python programs, including loops, list operations, and more, with detailed explanations for each. Use these examples to enhance your understanding of Python programming.

1. Simple For Loop

This loop prints numbers from 0 to 4.

for i in range(5):
    print(i)

Explanation:
1. for i in range(5): – Loops through numbers from 0 to 4.
2. print(i) – Prints the current number.

2. Iterate Through a String

This loop iterates through each character in a string.

val = 'Pavilion'
for i in val:
    print(i)

Explanation:
1. val = 'Pavilion' – Defines the string.
2. for i in val: – Loops through each character.
3. print(i) – Prints each character.

3. Range Function

This loop prints numbers from 1 to 5.

for i in range(1, 6):
    print(i)

Explanation:
1. for i in range(1, 6): – Loops through numbers from 1 to 5.
2. print(i) – Prints the current number.

4. Range with Step

This loop prints every second number from 1 to 5.

for i in range(1, 6, 2):
    print(i)

Explanation:
1. for i in range(1, 6, 2): – Loops through numbers from 1 to 5 with a step of 2.
2. print(i) – Prints the current number.

5. Iterate Through a String

This loop prints each character of the string 'Dominic'.

a = 'Dominic'
for i in a:
    print(i)

Explanation:
1. a = 'Dominic' – Defines the string.
2. for i in a: – Loops through each character.
3. print(i) – Prints each character.

6. Print Numbers 1-10

This loop prints numbers from 1 to 10.

for i in range(1, 11):
    print(i)

Explanation:
1. for i in range(1, 11): – Loops through numbers from 1 to 10.
2. print(i) – Prints the current number.

7. Print Numbers 25-50

This loop prints numbers from 25 to 50.

for i in range(25, 51):
    print(i)

Explanation:
1. for i in range(25, 51): – Loops through numbers from 25 to 50.
2. print(i) – Prints the current number.

8. Print Even Numbers from 1-10

This loop prints even numbers between 1 and 10.

for i in range(1, 11):
    if i % 2 == 0:
        print(i)

Explanation:
1. for i in range(1, 11): – Loops through numbers from 1 to 10.
2. if i % 2 == 0: – Checks if the number is even.
3. print(i) – Prints the number if it is even.

9. Print Fruits from List

This loop prints each item in a list of fruits.

fruits = ['Apple', 'Orange', 'Grapes', 'Guva']
for i in fruits:
    print(i)

Explanation:
1. fruits = ['Apple', 'Orange', 'Grapes', 'Guva'] – Defines the list.
2. for i in fruits: – Loops through each item in the list.
3. print(i) – Prints each item.

10. Print Even Indexed Items from List

This loop prints items at even indices from a list.

fruits = ['Apple', 'Orange', 'Grapes', 'Guva']
for i in range(len(fruits)):
    if i % 2 == 0:
        print(fruits[i])

Explanation:
1. fruits = ['Apple', 'Orange', 'Grapes', 'Guva'] – Defines the list.
2. for i in range(len(fruits)): – Loops through indices of the list.
3. if i % 2 == 0: – Checks if the index is even.
4. print(fruits[i]) – Prints the item at the even index.

11. Print Even Indexed Characters from String

This loop prints characters at even indices from a string.

string = 'Laptop'
for i in range(len(string)):
    if i % 2 == 0:
        print(string[i])

Explanation:
1. string = 'Laptop' – Defines the string.
2. for i in range(len(string)): – Loops through indices of the string.
3. if i % 2 == 0: – Checks if the index is even.
4. print(string[i]) – Prints the character at the even index.

12. Multiplication Table

This program prints a multiplication table for a given number.

number = int(input('Enter the number: '))
for i in range(1, 11):
    print(number, 'x', i, '=', number * i)

Explanation:
1. number = int(input('Enter the number: ')) – Takes user input for the number.
2. for i in range(1, 11): – Loops through numbers from 1 to 10.
3. print(number, 'x', i, '=', number * i) – Prints the multiplication result.

13. Print Reversed String

This program prints a given string in reverse order.

string = 'Python'
b = ''
for i in string:
    b = i + b
print(b)

Explanation:
1. string = 'Python' – Defines the string.
2. b = '' – Initializes an empty string for reversal.
3. for i in string: – Loops through each character.
4. b = i + b – Prepends each character to the result string.
5. print(b) – Prints the reversed string.

14. Calculate Factorial

This program calculates the factorial of a given number.

num = int(input('Enter a number: '))
factorial = 1
for i in range(1, num + 1):
    factorial *= i
print('The factorial of', num, 'is', factorial)

Explanation:
1. num = int(input('Enter a number: ')) – Takes user input for the number.
2. factorial = 1 – Initializes the factorial variable.
3. for i in range(1, num + 1): – Loops from 1 to the number.
4. factorial *= i – Multiplies each number to calculate the factorial.
5. print('The factorial of', num, 'is', factorial) – Prints the result.

15. Find Length of Strings in List

This program prints the length of each string in a list.

string_list = ['cat', 'window', 'statement', 'step', 'number']
for string in string_list:
    print('The length of', string, 'is', len(string))

Explanation:
1. string_list = ['cat', 'window', 'statement', 'step', 'number'] – Defines the list.
2. for string in string_list: – Loops through each string.
3. print('The length of', string, 'is', len(string)) – Prints the length of each string.

16. Find Squares of Numbers in List

This program prints the square of each number in a list.

num_list = [1, 2, 3, 4, 5]
for number in num_list:
    print('The square of', number, 'is', number * number)

Explanation:
1. num_list = [1, 2, 3, 4, 5] – Defines the list of numbers.
2. for number in num_list: – Loops through each number.
3. print('The square of', number, 'is', number * number) – Prints the square of each number.

17. Sum of Numbers in List

This program calculates the sum of numbers in a list.

num_list = [10, 20, 30, 40, 50]
total_sum = 0
for number in num_list:
    total_sum += number
print('The total sum is:', total_sum)

Explanation:
1. num_list = [10, 20, 30, 40, 50] – Defines the list of numbers.
2. total_sum = 0 – Initializes the sum variable.
3. for number in num_list: – Loops through each number.
4. total_sum += number – Adds each number to the total sum.
5. print('The total sum is:', total_sum) – Prints the total sum.

18. Sum of Odd Numbers in Range

This program calculates the sum of odd numbers from 1 to 20.

total_sum = 0
for number in range(1, 21):
    if number % 2 != 0:
        total_sum += number
print('The sum of odd numbers from 1 to 20 is:', total_sum)

Explanation:
1. total_sum = 0 – Initializes the sum variable.
2. for number in range(1, 21): – Loops through numbers from 1 to 20.
3. if number % 2 != 0: – Checks if the number is odd.
4. total_sum += number – Adds each odd number to the total sum.
5. print('The sum of odd numbers from 1 to 20 is:', total_sum) – Prints the total sum.

19. Print Even Numbers from 1-20

This program prints even numbers between 1 and 20.

for number in range(1, 21):
    if number % 2 == 0:
        print(number)

Explanation:
1. for number in range(1, 21): – Loops through numbers from 1 to 20.
2. if number % 2 == 0: – Checks if the number is even.
3. print(number) – Prints the even number.

20. Reverse a List

This program prints the items of a list in reverse order.

my_list = [1, 2, 3, 4, 5]
for item in reversed(my_list):
    print(item)

Explanation:
1. my_list = [1, 2, 3, 4, 5] – Defines the list.
2. for item in reversed(my_list): – Loops through the list in reverse.
3. print(item) – Prints each item in reverse order.

No comments:

Post a Comment

Search here..

Adbox