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

Python Code Examples for Beginners

The document contains several Python code snippets demonstrating different programming concepts. These include generating a custom star pattern, calculating the LCM of two numbers, counting vowels in a string, using a while-else loop, counting lines, words, and characters in a file, and generating a Fibonacci series. Each section provides example inputs and outputs for clarity.

Uploaded by

dhanananjay88
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)
13 views2 pages

Python Code Examples for Beginners

The document contains several Python code snippets demonstrating different programming concepts. These include generating a custom star pattern, calculating the LCM of two numbers, counting vowels in a string, using a while-else loop, counting lines, words, and characters in a file, and generating a Fibonacci series. Each section provides example inputs and outputs for clarity.

Uploaded by

dhanananjay88
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

# 1.

Custom Star Pattern


# Output:
# *
# **
# *****
# ************
pattern = [1, 2, 5, 12]
for count in pattern:
print('*' * count)

# 2. LCM of two numbers


# Example Input:
# Enter first number: 3
# Enter second number: 5
# Output: LCM is 15
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
max_num = max(a, b)
while True:
if max_num % a == 0 and max_num % b == 0:
print("LCM is", max_num)
break
max_num += 1

# 3. Count vowels in string


# Output: 6
def count1(s):
vowels = "AEIOUaeiou"
count = 0
for c in s:
if c in vowels:
count += 1
return count
print(count1('I love India'))

# 4. While-else loop
# Output:
# 0
# 1
# 2
# 0
i = 0
while i < 3:
print(i)
i += 1
else:
print(0)

# 5. Count lines, words, characters in file


# Suppose [Link] contains:
# Hello world
# This is Python
# Output:
# Total Lines: 2
# Total Words: 5
# Total Characters: 29
def count_file_content(filename):
try:
with open(filename, 'r') as file:
lines = [Link]()
line_count = len(lines)
word_count = 0
char_count = 0
for line in lines:
word_count += len([Link]())
char_count += len(line)
print("Total Lines:", line_count)
print("Total Words:", word_count)
print("Total Characters:", char_count)
except FileNotFoundError:
print("File not found. Please check the filename.")

count_file_content("[Link]")

# 6. Fibonacci series
# Example Input: 5
# Output: 0 1 1 2 3
n = int(input("Enter number of terms: "))
a, b = 0, 1
for _ in range(n):
print(a, end=' ')
a, b = b, a + b

You might also like