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

Python Cheat Sheet

This document is a Python Basic Code Cheat Sheet that provides essential code snippets for user input, output, decision making, loops, functions, arithmetic operations, and various checks such as even/odd, prime, palindrome, factorial, and leap year. Each section includes example code to demonstrate the concepts. It serves as a quick reference for basic Python programming tasks.

Uploaded by

parth20106
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)
15 views3 pages

Python Cheat Sheet

This document is a Python Basic Code Cheat Sheet that provides essential code snippets for user input, output, decision making, loops, functions, arithmetic operations, and various checks such as even/odd, prime, palindrome, factorial, and leap year. Each section includes example code to demonstrate the concepts. It serves as a quick reference for basic Python programming tasks.

Uploaded by

parth20106
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
You are on page 1/ 3

Python Basic Code Cheat Sheet

1. Input from User


name = input("Enter your name: ")
number = int(input("Enter a number: "))

2. Output/Print
print("Hello, World!")
print("The number is:", number)

3. If-Else (Decision Making)


if number > 0:
print("Positive number")
else:
print("Not a positive number")

4. Elif (Else If)


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

5. For Loop
for i in range(1, 6):
print(i)

6. While Loop
i = 1
while i <= 5:
print(i)
i += 1

7. Function Definition
def greet():
print("Hello!")

greet()

8. Arithmetic Operations
a = 10
Python Basic Code Cheat Sheet

b = 5
print(a + b) # Addition
print(a - b) # Subtraction
print(a * b) # Multiplication
print(a / b) # Division
print(a % b) # Modulus

9. Check Even or Odd


num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")

10. Check Prime


num = int(input("Enter a number: "))
if num > 1:
for i in range(2, num):
if num % i == 0:
print("Not Prime")
break
else:
print("Prime")
else:
print("Not Prime")

11. Check Palindrome


text = input("Enter text: ")
if text == text[::-1]:
print("Palindrome")
else:
print("Not Palindrome")

12. Find Factorial


num = int(input("Enter a number: "))
fact = 1
i = 1
while i <= num:
fact *= i
i += 1
print("Factorial is", fact)

13. Leap Year Check


year = int(input("Enter a year: "))
Python Basic Code Cheat Sheet

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


print("Leap year")
else:
print("Not a leap year")

You might also like