Skip to content

Saim2026/Python_Basics

Repository files navigation

Task#1: Write a Python program to check if a number is even or odd

✅ How it works:

  1. % is the modulus operator, which gives the remainder when one number is divided by another.

  2. num % 2 == 0 means the number is divisible by 2 → even.

  3. If the remainder is not 0 → odd.

  4. End

✅ Examples

Enter a number: 7 , 7 is odd.

Enter a number: 12 , 12 is even.


Task#2: Write a Python program to find the factorial of a number using a loop:

✅ How it works:

  1. Factorial of a number n is calculated as: n! = n × (n-1) × (n-2) × ... × 1

  2. Initialize factorial = 1 because multiplying by 1 doesn’t change the value.

  3. Use a loop from 1 to num to multiply the values step by step.

    📌Special cases:

    0! = 1

    Factorial is not defined for negative numbers.

✅ Examples

Enter a non-negative integer: 5, The factorial of 5 is 120

Enter a non-negative integer: 0 , The factorial of 0 is 1


Task#3: Write a Python program to check if a number is prime

✅ How it works:

A prime number is a number greater than 1 and divisible only by 1 and itself.

We check divisibility from 2 to sqrt(num) because a larger factor would have a corresponding smaller factor.

If any divisor is found → not prime.

If loop completes without breaking → prime.

📌 Notes:

1, 0, and negative numbers are not prime.

✅ Example

Enter a number: 7, 7 is a prime number.

Enter a number: 10 , 10 is not a prime number.


Task#4: Write a Python program to reverse a string

✅ How it works:

  1. Read the string from the user and store it in variable text

  2. Initialize an empty string reversed_text

  3. Repeat the following steps for each character char (loop variable) in text (from left to right):

  4. Set reversed_text = char + reversed_text (This adds the current character to the front of the reversed string)

  5. After the loop ends, reversed_text will contain the reversed string

  6. Print reversed_text

  7. End

✅ Example

Input: "hello" , Output: "olleh"

📌 What happens exactly?

If text = "hello":

1st Iteration: 'h' 'h'

2nd Iteration: 'e' 'eh'

3rd Iteration: 'l' 'leh'

4th Iteration: 'l' 'lleh'

5th Iteration: 'o' 'olleh'

So the first time the loop runs, " " will be the first character of the string.


Task#5: Write a Python program to check if a string is a palindrome.

What is a palindrome?

A string that reads the same forward and backward, e.g.:

level , noon , 121 , A man a plan a canal Panama

✅ How it works:

  1. Read a string from the user and store it in variable text

  2. Remove all spaces from text and convert it to lowercase, with the help of functions text.replace(" ", "").lower()

  3. Store the result in clean_text

  4. Initialize an empty string reversed_text

  5. For each character char in clean_text, repeat:

    • Set reversed_text = char + reversed_text (This builds the reversed string)
  6. Compare clean_text with reversed_text, If both are equal:

    • Print "It is a palindrome!"

    Else:

    • Print "It is not a palindrome."
  7. End

✅ Example

Input: level

→ Reversed: level

→ Output: It is a palindrome!


Task#6: Write a Python program to count the number of vowels in a string

✅ How it works:

  1. Converts the string to lowercase so both A and a are counted the same.

  2. Loops through each character and checks if it is a vowel.

  3. Increments count whenever a vowel is found.

✅ Example

Input: Hello World

Output: Number of vowels: 3


Task#7: Write a Python program to find the length of a string.

✅ How it works:

  1. Read a string from the user and store it in variable text

  2. Initialize a counter variable count to 0

  3. For each character char in text, repeat:

    • Increase count by 1
  4. After the loop ends, count will store the total number of characters

  5. Print the value of count

  6. End

✅ Example

Input: hello

Loop runs 5 times → count = 5

Output: Length of the string: 5


Task#8: Write a Python program to find the common characters between two strings.

✅ How it works:

  1. Read the first string and store it in str1

  2. Read the second string and store it in str2

  3. Initialize an empty string common to store the common characters

  4. For each character char in str1, do:

    • If char exists in str2 and char is not already in common:
      • Append char to common
  5. After the loop ends, common contains all unique common characters

  6. Print common

  7. End

✅ Example

Input: str1 = "banana" , str2 = "cabana"

Output: ban


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages