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

Python Homework: Variables & Functions

The document outlines a Python homework assignment focused on practicing variables, functions, string formatting, and list methods. It includes specific tasks such as creating variables of different types, user input handling, string manipulation, and list operations. Additional resources for further learning are also provided.
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)
6 views2 pages

Python Homework: Variables & Functions

The document outlines a Python homework assignment focused on practicing variables, functions, string formatting, and list methods. It includes specific tasks such as creating variables of different types, user input handling, string manipulation, and list operations. Additional resources for further learning are also provided.
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

Python Homework

Homework Tasks

This homework will help you practice the topics we covered in the last lesson:

- Variables and their types: int, float, bool, string, and list.

- Basic functions: print(), input(), type(), int(), str().

- String formatting and methods.

- List methods.

Tasks:

1. Create variables of each type (int, float, bool, string, list). Print their types using the type() function.

2. Write a program that asks the user to input their age using input(). Convert the input to an integer using

int() and print a message: 'You are X years old!', where X is the user's age.

3. Practice string formatting: Create a variable with your name and age. Print the sentence: 'My name is

[Name] and I am [Age] years old.' using formatted strings.

4. Write a program to demonstrate at least three string methods (e.g., upper(), lower(), replace()).

5. Create a list of your favorite fruits and print the list. Add a new fruit to the list, remove one, and print the

updated list.

6. Create a program that accepts a list of numbers (as input, comma-separated) from the user, and then

converts it into a list. Print the largest and smallest number in the list.

7. Explore the slicing of strings and lists: Print the first 3 characters of a string and the last 2 elements of a

list.

Additional Resources:

For more information about variables and their types, you can refer to the following article:

Page 1
Python Homework

[Link]

Practice string and list methods here:

[Link]

[Link]

Page 2

Common questions

Powered by AI

Understanding variables and data types is crucial as it determines how values can be stored and manipulated. In Python, knowing whether a variable is an int, float, string, or list affects permissible operations, impacts memory usage, and controls function applicability, ensuring correct operation and preventing errors .

String and list methods streamline coding by providing built-in functionality to manipulate data efficiently. Methods like append(), remove(), lower(), and replace() minimize the need for complex logic, enhance performance, and improve code readability and maintainability by reducing boilerplate code .

String and list slicing in Python allows accessing specific parts of the data. For a string 's = "abcdef"', 's[:3]' will give 'abc', taking the first three characters. For a list 'lst = [1, 2, 3, 4, 5]', 'lst[-2:]' provides [4, 5], which are the last two elements .

In Python, 'print()' outputs data to the screen, 'input()' receives input from the user, 'type()' returns the type of a variable, 'int()' converts a value to an integer, and 'str()' converts to a string. Together, they allow for interactive programs that can process and display user data efficiently .

String formatting allows you to insert variables into strings. Use the f-string method by placing an 'f' before the string and using curly braces to include variable names directly within the string. For example, if you have 'name = "John"' and 'age = 30', you can format it as 'f"My name is {name} and I am {age} years old."' which would output 'My name is John and I am 30 years old.' .

You can create variables of different types in Python, such as 'int' for integers, 'float' for floating-point numbers, 'bool' for boolean values, 'string' for text, and 'list' for collections of items. You can print their types using the type() function to verify. For example, create variables like 'x = 5' (int), 'y = 5.5' (float), 'flag = True' (bool), 'name = "Alice"' (string), and 'fruits = ["apple", "banana"]' (list). Calling type(x), type(y), type(flag), type(name), and type(fruits) will confirm their types as int, float, bool, string, and list respectively .

To convert a comma-separated string to a list of numbers in Python, first use the input() function to take the string and split it using the split(',') method. Convert each element to an integer using map(int, ...). With the list of numbers, use the max() function to find the largest number and min() function for the smallest number. For example, if user inputs '"1,2,3,4"', 'numbers = list(map(int, input().split(',')))' converts this to [1, 2, 3, 4]. Then 'max(numbers)' and 'min(numbers)' yield 4 and 1 respectively .

Effectively using documentation involves understanding the structure of Python's official documentation or trusted tutorials, following code examples, and practicing exercises provided. Resources like the Python data structure documentation or Real Python articles provide comprehensive insights and practical examples, crucial for mastering concepts like string and list methods .

Common string methods in Python include 'upper()', 'lower()', and 'replace()'. In a program, you might have a string 'text = "Hello World"' and demonstrate upper() by 'text.upper()' to return 'HELLO WORLD', lower() by 'text.lower()' to return 'hello world', and replace() by 'text.replace("World", "Python")' to return 'Hello Python' .

In Python, lists can be manipulated using methods like 'append()' to add items and 'remove()' to delete items. For instance, with a list 'fruits = ["apple", "orange"]', use 'fruits.append("banana")' to add 'banana', resulting in ['apple', 'orange', 'banana']. Use 'fruits.remove("orange")' to remove 'orange', resulting in ['apple', 'banana'].

You might also like