0% found this document useful (0 votes)
4 views

pyhton

The document contains various Python code snippets demonstrating basic programming concepts such as printing 'Hello, World!', calculating factorials, checking leap years, generating Fibonacci series, and identifying prime numbers. It also includes examples of working with even numbers, comparing integers, checking for palindromes, and printing star patterns. Each code snippet is accompanied by user input prompts and outputs relevant results.

Uploaded by

Nikhil Marri
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

pyhton

The document contains various Python code snippets demonstrating basic programming concepts such as printing 'Hello, World!', calculating factorials, checking leap years, generating Fibonacci series, and identifying prime numbers. It also includes examples of working with even numbers, comparing integers, checking for palindromes, and printing star patterns. Each code snippet is accompanied by user input prompts and outputs relevant results.

Uploaded by

Nikhil Marri
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Python code for Writing “Hello, World!

” program:

print("Hello, World!")

1. Binary to decimal
2. 11011 to 26
3. Eliminate vowels and consonants in a string

Python code for displaying the list vertical of even numbers:

n = int(input("Enter the range: "))

for i in range(2, n+1, 2):

print(i)

python code for displaying all even in a set of even numbers list

n = int(input("Enter the limit: ")) # take user input for the limit of
the list

# use a list comprehension to create a list of even numbers up to the


limit entered by the user
even_list = [i for i in range(1, n+1) if i % 2 == 0]

# print the even list


print("The list of even numbers up to", n, "is:", even_list)

Python code for finding the factorial of a given number:

def factorial(n):

if n == 0:

return 1

else:
return n * factorial(n-1)

n = int(input("Enter a number: "))

print("Factorial of", n, "is", factorial(n))

Python code for comparing two numbers using the else-if statement and
output smaller and larger numbers:

a = int(input("Enter first number: "))


b = int(input("Enter second number: "))

if a > b:
print(a, "is greater than", b)
elif a < b:
print(a, "is less than", b)
else:
print(a, "is equal to", b)

Python code for determining if the given year is a leap year:

year = int(input("Enter a year: "))

if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:

print(year, "is a leap year")

else:

print(year, "is not a leap year")


Python code for printing all the elements in the Fibonacci number series
using recursion technique:

def fibonacci(n):

if n <= 1:

return n

else:

return fibonacci(n-1) + fibonacci(n-2)

n = int(input("Enter the number of terms: "))

for i in range(n):

print(fibonacci(i))

Python code for checking if the number is a palindrome number or not:

n = input("Enter a number: ")

if n == n[::-1]:

print(n, "is a palindrome number")

else:

print(n, "is not a palindrome number")


Python code for generating all the prime numbers between one & the given
number:

n = int(input("Enter a number: "))

for i in range(2, n+1):

for j in range(2, i):

if i % j == 0:

break

else:

print(i)

Python code for printing the pyramid of stars using nested for loops:

n = int(input("Enter the number of rows: "))

for i in range(n):

for j in range(n-i-1):

print(" ", end="")

for j in range(i*2+1):

print("*", end="")

print()

Python code for printing reversed pyramid on the programming console


using for loops & decrement operator:
n = int(input("Enter the number of rows: "))

for i in range(n, 0, -1):

for j in range(n-i):

print(" ", end="")

for j in range(i*2-1):

print("*", end="")

print()

You might also like