Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Python Basic Structures Lists, Strings, And Files

The document outlines various Python programming exercises across three modules, focusing on basic structures such as lists, strings, and file handling. Each module contains multiple code snippets demonstrating different functionalities, including input manipulation, data processing, and file reading. The exercises emphasize practical applications of Python syntax and logic for beginners.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Basic Structures Lists, Strings, And Files

The document outlines various Python programming exercises across three modules, focusing on basic structures such as lists, strings, and file handling. Each module contains multiple code snippets demonstrating different functionalities, including input manipulation, data processing, and file reading. The exercises emphasize practical applications of Python syntax and logic for beginners.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Python Basic Structures: Lists, Strings, and Files

1st Module
a.
import sys

numbers = sys.argv[1:]
for i in range(len(numbers)):
numbers[i] = int(numbers[i])
for i in range(len(numbers)):
if numbers[i] > 10:
numbers[i] = '*'

print(numbers)

b.
import sys

my_list = sys.argv[1:]

if len(my_list) < 5:
print(my_list * 3)
else:
print(my_list)

c.
import sys

strings = sys.argv[1:]

strings.sort()
print(strings[0])

d.
import sys

numbers = sys.argv[1:]
for i in range(len(numbers)):
numbers[i] = int(numbers[i])

next_number = numbers[-1] + 1
numbers.append(next_number)
numbers.append(next_number + 1)

print(numbers)
e.
import sys

number = int(sys.argv[1])
data = [[0 for i in range(number)] for j in range(number)]

for i in range(number):
data[i][i] = 1

# Print the 2D list row by row without square brackets and


commas
for row in data:
print(" ".join(map(str, row)))

2nd Module
a.
# Get user input
user_input = input()

# Get the first and last character


first_char = user_input[0]
last_char = user_input[-1]
# Print the output with context
print(f"{first_char} is the first character and {last_char} is the
last character")

b.
# Get user input
user_input = input()

# Get the length of the user input


length = len(user_input)

# Print the string repeated by its length for the same number
of lines
for _ in range(length):
print(user_input * length)

c.
txt = input()
second_string = ""
for char in txt:
if char.islower():
second_string += "l"
elif char.isupper():
second_string += "u"
else:
second_string += "-"

print(second_string)

d.
# Get user input
txt = input()

# Find the midpoint of the string


midpoint = len(txt) // 2

# Print the first half and the second half


print(txt[:midpoint])
print(txt[midpoint:])

e.
# Get user input
txt = input()
# Initialize the swapped string
swapped = ""

# Swap characters two at a time


for i in range(0, len(txt), 2):
swapped += txt[i+1] + txt[i]

# Print the swapped string


print(swapped)

3rd Module
a.
import sys

test_file = sys.argv[1]

with open(test_file, "r") as file:


lines = file.readlines()
num_lines = len(lines)
num_chars = sum(len(line) for line in lines)
print(f"{num_lines} lines")
print(f"{num_chars} characters")

b.
import sys

test_file = sys.argv[1]

with open(test_file, "r") as file:


lines = file.readlines()

# Initialize variables to hold the sum of each column


sums = [0, 0, 0, 0]
num_rows = 0

# Loop through each line in the CSV file


for line in lines:
# Split the line by commas and convert to floats
columns = list(map(float, line.strip().split(',')))
# Add the values to the corresponding sum
for i in range(4):
sums[i] += columns[i]
num_rows += 1

# Calculate and print the average for each column


averages = [s / num_rows for s in sums]
print(" ".join(map(str, averages)))

c.
import sys

test_file = sys.argv[1]

with open(test_file, "r") as file:


lines = file.readlines()

# Reverse the lines and print them


for line in reversed(lines):
print(line.strip())

d.
import sys, csv

test_file = sys.argv[1]
with open(test_file, "r") as file:
reader = csv.reader(file, delimiter='\t')
next(reader) # Skip the header row
oldest_person = None
oldest_age = -1

# Loop through the rows and find the oldest person


for row in reader:
name, age, career = row
age = int(age) # Convert age to an integer
if age > oldest_age:
oldest_age = age
oldest_person = name

print(f"The oldest person is {oldest_person}.")

e.
import sys, csv

test_file = sys.argv[1]
with open(test_file, "r") as file:
reader = csv.reader(file)
next(reader) # Skip the header row
cities_southern_hemisphere = []

# Loop through the rows and find cities in the Southern


Hemisphere
for row in reader:
city, country, latitude, longitude = row
latitude = float(latitude) # Convert latitude to a float
if latitude < 0: # Check if the latitude is in the Southern
Hemisphere
cities_southern_hemisphere.append(city)

# Print the cities in the Southern Hemisphere


print(f"The following cities are in the Southern Hemisphere:
{', '.join(cities_southern_hemisphere)}.")

You might also like