0% found this document useful (0 votes)
10 views5 pages

Python Exercises

The document contains exercises on Python variables, numbers, strings, and lists. It includes code snippets demonstrating variable assignment, calculations, string manipulation, and list operations. Additionally, it highlights invalid variable names and showcases the use of formatted strings and list methods.

Uploaded by

ripdelta54
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Python Exercises

The document contains exercises on Python variables, numbers, strings, and lists. It includes code snippets demonstrating variable assignment, calculations, string manipulation, and list operations. Additionally, it highlights invalid variable names and showcases the use of formatted strings and list methods.

Uploaded by

ripdelta54
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

### Exercise: Python Variables

Q1

```python

break = 5

```

Q2

```python

birth_year = 2000

current_year = 2025

age = current_year - birth_year

print(age)

```

Q3

```python

first = "John"

middle = "A."

last = "Doe"

print(first + " " + middle + " " + last)

```

Q4

Invalid variable names:


1record

record-one

record^one

continue

### Exercise: Numbers in Python

Q1

```python

length = 92

width = 48.8

area = length * width

print(area)

```

Q2

```python

packets = 9

cost_per_packet = 1.49

total_cost = packets * cost_per_packet

change = 20 - total_cost

print(change)

```

Q3
```python

length = 5.5

cost_per_sqft = 500

area = length ** 2

total_cost = area * cost_per_sqft

print(total_cost)

```

Q4

```python

print(bin(17))

```

### Exercise: String in Python

Q1

```python

street = "123 Main St"

city = "New York"

country = "USA"

address1 = street + "\n" + city + "\n" + country

print(address1)

address2 = f"{street}\n{city}\n{country}"
print(address2)

```

Q2

```python

s = "Earth revolves around the sun"

print(s[6:14])

print(s[-3:])

```

Q3

```python

fruits = 2

veggies = 3

print(f"I eat {veggies} veggies and {fruits} fruits daily")

```

Q4

```python

s = 'maine 200 banana khaye'

print([Link]('200', '10').replace('banana', 'samosa'))

```

### Exercise: Python Lists


Q1

```python

expenses = [2200, 2350, 2600, 2130, 2190]

print(expenses[1] - expenses[0])

print(sum(expenses[:3]))

print(2000 in expenses)

[Link](1980)

expenses[3] -= 200

print(expenses)

```

Q2

```python

heros = ['spider man', 'thor', 'hulk', 'iron man', 'captain america']

print(len(heros))

[Link]('black panther')

[Link]('black panther')

[Link](3, 'black panther')

heros[1:3] = ['doctor strange']

[Link]()

print(heros)

```

You might also like