0% found this document useful (0 votes)
33 views3 pages

Python Basics: Even, Odd, Factorial, Palindrome

The document contains a Jupyter Notebook with basic Python programming problems, including checking if a number is even or odd, determining if a number is positive, negative, or zero, calculating the factorial of a number, reversing a number, checking for palindromes, generating Fibonacci numbers, and printing a pattern of numbers. Each problem is accompanied by code snippets demonstrating the solution. The notebook serves as a practical guide for beginners to learn fundamental programming concepts in Python.

Uploaded by

ranjitmajumdar69
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views3 pages

Python Basics: Even, Odd, Factorial, Palindrome

The document contains a Jupyter Notebook with basic Python programming problems, including checking if a number is even or odd, determining if a number is positive, negative, or zero, calculating the factorial of a number, reversing a number, checking for palindromes, generating Fibonacci numbers, and printing a pattern of numbers. Each problem is accompanied by code snippets demonstrating the solution. The notebook serves as a practical guide for beginners to learn fundamental programming concepts in Python.

Uploaded by

ranjitmajumdar69
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

9/8/25, 3:30 PM Python basic problems - Jupyter Notebook

In [ ]: #Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use o

In [ ]: #check even odd

In [ ]: num = int(input("Enter a number: "))


if (num % 2) == 0:
print(num, " is even")
else:
print(num, " is odd")

In [ ]: #check positive, negative or 0

In [ ]: num = float(input("Enter a number: "))


if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")

In [ ]: #Factorial of a number

In [ ]: ​
factorial = 1
# check if the number is negative, positive or zero
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)

In [ ]: #Reversing a number

In [ ]: n=int(input("Enter number: "))


rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
print("Reverse of the number:",rev)

In [ ]: #Check if it is a palindrome

localhost:8888/notebooks/Python basic [Link] 1/3


9/8/25, 3:30 PM Python basic problems - Jupyter Notebook

In [ ]: n=int(input("Enter number:"))
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")

In [ ]: #fibonacci - 0 1 1 2 3 5 8.......

In [16]: n=int(input("Enter number:"))


a = 0
b = 1
if n < 0:
print("Incorrect input")
elif n == 0:
print(a)
elif n == 1:
print(a)
else:
for i in range(2,n):
c = a + b
a = b
b = c
print(b)

Enter number:3
1

In [2]: # 10 is the total number to print


for num in range(6):
for i in range(num):
print (num,end=" ") #print number
# new line after each row to display pattern correctly
print("\n")

2 2

3 3 3

4 4 4 4

5 5 5 5 5

In [ ]: ​

localhost:8888/notebooks/Python basic [Link] 2/3


9/8/25, 3:30 PM Python basic problems - Jupyter Notebook

localhost:8888/notebooks/Python basic [Link] 3/3

Common questions

Powered by AI

The document ensures computational efficiency by using simple arithmetic operations and loops with linear complexity relative to the number of digits in the number. These operations, such as digit extraction and manipulation, are performed in constant time per iteration, ensuring overall efficiency in checking palindromes and reversing numbers .

The document calculates the factorial of a number by initializing a variable called 'factorial' to 1. It checks if the number is non-negative, iterating from 1 to the number, and multiplies 'factorial' by each integer in the range, resulting in the factorial value. If the number is zero, it states that the factorial is 1 by definition .

The document outlines an algorithm that repeatedly extracts the last digit of the number, appends it to a new number initialized to zero, and removes the last digit from the original number by integer division by 10. This process repeats until the original number becomes zero. This ensures correctness by systematically reversing the digits .

The document emphasizes step-by-step execution within loops, using simple arithmetic and control structures that parallel the concept being demonstrated. This structural clarity facilitates a deeper understanding of operational flow, aiding in learning by practice and easing debugging processes .

The document uses nested loops to print a pattern of numbers where the outer loop controls the number of lines, and the inner loop prints the line's respective number. The inner loop iterates as many times as the current line number, thus forming a triangle pattern that increases with each row .

The document provides a solution where two initial Fibonacci numbers, 0 and 1, are iteratively summed to generate the next terms. This process continues until the specified number of terms is reached, accommodating edge cases for negative, zero, or single count inputs .

The document outlines a method where a given number is compared to zero. If the number is greater than zero, it is positive; if it equals zero, the output is 'Zero'; and if it is less than zero, it is negative .

The document checks for a palindrome by reversing the input number using a digit extraction and reversal process, then comparing the reversed number with the original. If they are equal, the number is a palindrome; otherwise, it is not .

The document describes checking if a number is even or odd by dividing the number by 2 and checking the remainder using the modulus operator. If the remainder is 0, the number is even; otherwise, it is odd .

The document handles different input cases by integrating conditions to check for negative numbers in factorial and Fibonacci computations, appropriately responding to invalid inputs, such as negative values for operations that require non-negative numbers, ensuring robustness in the code snippets .

You might also like