0% found this document useful (0 votes)
21 views2 pages

Essential Python Programs for Beginners

The document contains basic Python programs that demonstrate fundamental programming concepts. It includes examples for printing, arithmetic operations, string manipulation, and list operations. Each program is presented with code snippets for easy understanding and implementation.

Uploaded by

rehmanyousaf3538
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)
21 views2 pages

Essential Python Programs for Beginners

The document contains basic Python programs that demonstrate fundamental programming concepts. It includes examples for printing, arithmetic operations, string manipulation, and list operations. Each program is presented with code snippets for easy understanding and implementation.

Uploaded by

rehmanyousaf3538
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

Basic Python Programs

1. Print 'Hello, World!'


print("Hello, World!")

2. Add two numbers


a = 5 b = 3 print("Sum:", a + b)

3. Check if a number is even or odd


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

4. Find the maximum of two numbers


a = 10 b = 20 print("Maximum:", max(a, b))

5. Calculate the area of a rectangle


length = 5 width = 3 area = length * width print("Area:", area)

6. Concatenate two strings


str1 = "Hello" str2 = "World" result = str1 + " " + str2 print(result)

7. Find the length of a string


string = "Python" print("Length:", len(string))

8. Check if a string is palindrome or not


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

9. Find the sum of all elements in a list


numbers = [1, 2, 3, 4, 5] print("Sum:", sum(numbers))

10. Find the maximum element in a list


numbers = [10, 25, 8, 99, 45] print("Maximum:", max(numbers))

11. Check if a number is positive, negative or zero


num = int(input("Enter a number: ")) if num > 0: print("Positive") elif num < 0:
print("Negative") else: print("Zero")

12. Find the factorial of a number


num = int(input("Enter a number: ")) fact = 1 for i in range(1, num + 1): fact *= i
print("Factorial:", fact)

13. Check if a string contains a specific word


text = "Python is awesome" word = "Python" if word in text: print("Word found!")
else: print("Word not found!")

14. Find the sum of squares of all numbers in a list


numbers = [1, 2, 3, 4] sum_squares = sum([x**2 for x in numbers]) print("Sum of
squares:", sum_squares)

15. Find the reverse of a string


string = "Python" print("Reversed:", string[::-1])

You might also like