0% found this document useful (0 votes)
2 views8 pages

Python Code Snippets for Beginners

The document provides Python code snippets for various tasks including joining strings with a separator, swapping numbers, checking if a number is even or odd, finding the largest of three numbers, calculating the factorial of a number, reversing a string, summing list elements, and counting vowels in a string. Each code snippet is followed by its expected output. These examples demonstrate basic programming concepts and operations in Python.

Uploaded by

mohsinsadrealam
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)
2 views8 pages

Python Code Snippets for Beginners

The document provides Python code snippets for various tasks including joining strings with a separator, swapping numbers, checking if a number is even or odd, finding the largest of three numbers, calculating the factorial of a number, reversing a string, summing list elements, and counting vowels in a string. Each code snippet is followed by its expected output. These examples demonstrate basic programming concepts and operations in Python.

Uploaded by

mohsinsadrealam
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

Print using # separator

text = ["Hello", "World", "Python", "Is", "Fun"]


print("#".join(text))

Output:
Hello#World#Python#Is#Fun#
Swap two numbers
a = 5
b = 10
a, b = b, a
print(a, b)

Output:
10 5
Check even or odd
num = 7
if num % 2 == 0:
print("Even")
else:
print("Odd")

Output:
Odd
Find largest of three
a, b, c = 3, 9, 6
print(max(a, b, c))

Output:
9
Find factorial
num = 5
fact = 1
for i in range(1, num+1):
fact *= i
print(fact)

Output:
120
Reverse a string
text = "Python"
print(text[::-1])

Output:
nohtyP
Sum list elements
nums = [10, 20, 5]
print(sum(nums))

Output:
35
Count vowels in a string
text = "Programming"
vowels = "aeiouAEIOU"
count = sum(1 for ch in text if ch in vowels)
print(count)

Output:
3

You might also like