Normal view
Task 8: Number Guessing Game
GIT configure in VSCode
Create a Github account:
https://github.com/pradeep-xxx
Create a repository and choose the generated link:
https://github.com/pradeep-xxx/python_workouts.git
Use in VS code Terminal:
PS C:\Users\para\Documents\PYTHON> git init
PS C:\Users\para\Documents\PYTHON> git config --global user.name "Pradeep xxx"
PS C:\Users\para\Documents\PYTHON> git config --global user.email "pradeep.xxxx@gmail.com"
PS C:\Users\para\Documents\PYTHON> git remote add origin https://github.com/pradeep-xxx/python_works.git
PS C:\Users\para\Documents\PYTHON> git add . [OR]
git add ToDoList.py ToDoList2.py todotask.txt todotask2.txt
PS C:\Users\para\Documents\PYTHON> git commit -m "Initial Commit" [OR]
PS C:\Users\para\Documents\PYTHON> git commit -m "Add selected Python and text files" [OR]
PS C:\Users\para\Documents\PYTHON> git commit -m "Add ToDoList.py, ToDoList2.py, todotask.txt, and todotask2.txt"
PS C:\Users\para\Documents\PYTHON> git push --set-upstream origin master [OR -1st time]
PS C:\Users\para\Documents\PYTHON> git push
Redis for Python - via Docker - No direct WSL
Redis doesnβt officially support native Windows installations anymore. Instead of setting up WSL (Windows Subsystem for Linux), Docker is the easier and more modern way to run Redis on a Windows machine.
Installing Docker Desktop gives you a full environment where you can run Redis (and many other tools) without friction.
πΉ Step 1: Install Docker Desktop
Download and install Docker Desktop from:
https://www.docker.com/products/docker-desktop/
Once installed:
Make sure Docker is running (look for the whale icon in your system tray).
Enable WSL2 integration if prompted during installation.
πΉ Step 2: Pull and Run Redis
Open PowerShell or Command Prompt and run:
docker run --name my-redis -p 6379:6379 -d redis
docker ps
πΉ Step 3: Connect to Redis
docker exec -it my-redis redis-cli
set name "DockerRedis"
get name
ping
Install RedisInsight and connect to:
https://redis.io/insight/
Host: localhost
Port: 6379
To find out whether your Windows PC uses an ARM64 or AMD64 (also called x64) architecture, follow these
from Command Prompt you can run:
C:\Users\pepsara>echo %PROCESSOR_ARCHITECTURE%
AMD64
C:\Users\pepsara>docker version
Client:
Version: 28.0.4
API version: 1.48
Go version: go1.23.7
Git commit: b8034c0
Built: Tue Mar 25 15:07:48 2025
OS/Arch: windows/amd64
Context: desktop-linux
Server: Docker Desktop 4.40.0 (187762)
Engine:
Version: 28.0.4
API version: 1.48 (minimum version 1.24)
Go version: go1.23.7
Git commit: 6430e49
Built: Tue Mar 25 15:07:22 2025
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.7.26
GitCommit: 753481ec61c7c8955a23d6ff7bc8e4daed455734
runc:
Version: 1.2.5
GitCommit: v1.2.5-0-g59923ef
docker-init:
Version: 0.19.0
GitCommit: de40ad0
C:\Users\pepsara>docker info
β
Step-by-Step: Use Redis with Python in VS Code
π§ 1. Install Redis client for Python
In your terminal (inside VS Code), run:
pip install redis
π§ͺ 2. Test Redis Connection in Python
Create a new Python file, e.g., redis_test.py, and add the following code:
import redis
Connect to Redis
r = redis.Redis(host='localhost', port=6379, db=0)
Set a key
r.set('mykey', 'Hello Redis!')
Get the key
value = r.get('mykey')
print(value.decode('utf-8')) # Output: Hello Redis!
Then run it: python redis_test.py
You should see: Hello Redis!
Task 6: Tuple
1.Create a tuple containing the names of three fruits. Print the tuple and its type.
2.Given a tuple t = ("apple", "banana", "cherry"), access and print the second element.
3.Unpack the tuple t = (1, 2, 3) into variables a, b, and c, and print the variables.
4.Concatenate two tuples t1 = (1, 2) and t2 = (3, 4) and print the result.
5.Create a tuple t = ("repeat",) and repeat it three times. Print the resulting tuple.
6.Given a tuple t = (1, 2, 3, 2, 2, 4), count the number of times the number 2 appears.
7.Given a tuple t = ("a", "b", "c", "d"), find the index of the element "c".
8.Check if the element 5 is present in the tuple t = (1, 2, 3, 4)
9.Find and print the length of the tuple t = ("one", "two", "three").
10.Slice the tuple t = (0, 1, 2, 3, 4, 5) to obtain a sub-tuple containing only the elements from index 2 to 4.
11.Create a nested tuple representing a 2D point (x, y) = ((1, 2), (3,
4)). Access and print the second coordinate of the second point.
12.Try to change the value of the first element in the tuple t = (1, 2, 3) and observe what happens.
13.Convert a list l = [1, 2, 3] to a tuple and print the result. Then convert a tuple t = (4, 5, 6) to a list and print the result.
14.Create a tuple with a single item 5 and verify its type is a tuple.
15.Iterate over the tuple t = ("ParottaSalna", "is", "good") and print each element.
16.Convert the string "hello" into a tuple of characters.
17.Convert a dictionary d = {"one": 1, "two": 2} into a tuple of its items.
18.Write a function that takes a tuple of numbers and returns the sum of the numbers.
19.Use tuples as keys in a dictionary to represent points on a grid. For example, grid = {(0, 0): "origin", (1, 2): "point A"}.
Task 5: Python Function
1.Write a function greet that takes a name as an argument and prints a greeting message.
2.Write a function sum_two that takes two numbers as arguments and returns their sum.
3.Write a function is_even that takes a number as an argument and returns True if the number is even, and False if it is odd.
4.Write a function find_max that takes two numbers as arguments and returns the larger one.
5.Write a function multiplication_table that takes a number n and prints the multiplication table for n from 1 to 10.
6.Write a function celsius_to_fahrenheit that takes a temperature in Celsius and returns the temperature in Fahrenheit.
7.Write a function power that takes two arguments, a number and an exponent, and returns the number raised to the given exponent. The exponent should have a default value of 2.
TASK 4: Python Lists
1.Create a list of five delivery items and print the third item in the list. eg: [βNotebookβ, βPencilβ, βEraserβ, βRulerβ, βMarkerβ]
2.A new delivery item βGlue Stickβ needs to be added to the list. Add it to the end of the list and print the updated list.
3.Insert βHighlighterβ between the second and third items and print the updated list.
4.One delivery was canceled. Remove βRulerβ from the list and print the updated list.
5.The delivery man needs to deliver only the first three items. Print a sublist containing only these items.
6.The delivery man has finished his deliveries. Convert all item names to uppercase using a list comprehension and print the new list.
7.Check if βMarkerβ is still in the list and print a message indicating whether it is found.
8.Print the number of delivery items in the list.
9.Sort the list of items in alphabetical order and print the sorted list.
10.The delivery man decides to reverse the order of his deliveries. Reverse the list and print it.
11.Create a list where each item is a list containing a delivery item and its delivery time. Print the first item and its time.
12.Count how many times βRulerβ appears in the list and print the count.
13.Find the index of βPencilβ in the list and print it.
14.Extend the list items with another list of new delivery items and print the updated list.
15.Clear the list of all delivery items and print the list.
16.Create a list with the item βNotebookβ repeated three times and print the list.
17.Using a nested list comprehension, create a list of lists where each sublist contains an item and its length, then print the new list.
18.Filter the list to include only items that contain the letter βeβ and print the filtered list.
19.Remove duplicate items from the list and print the list of unique items.
TASK 3: Slicing & Indexing
1.Write a function that takes a string and returns a new string consisting of its first and last character.
2.Write a function that reverses a given string.
3.Given a string, extract and return a substring from the 3rd to the 8th character (inclusive).
4.Write a function that returns every second character from a given string.
5.Write a function that replaces the middle third of a string with asterisks. If the length of the string is not divisible by 3, adjust the middle third accordingly.
6.Write a function that checks if a given string is a palindrome (reads the same backward as forward).
7.Given an email address, extract and return the domain.
Ans: How to find incase there are multiple occurrences? Loop?
8.Write a function that returns every third character from a given string.
9.Write a function that extracts and returns characters at even indices from a given string.
10.Write a function that skips every second character and then reverses the resulting string.
TASK 2: Constants and Variables
1.Create a variable named name and assign your name to it. Then print the value of the variable.
2.Create a variable age and assign your age to it. Later, reassign the variable with a new value and print the new value.
3.Assign the values 5, 10, and 15 to three variables a, b, and c in a single line. Print their values.
Ans:TypeError on 4th statement, because we're trying to concatenate integers (a, b, c) with strings
4.Swap the values of two variables x and y without using a third variable. Print their values before and after swapping.
5.Define constants PI with appropriate values and print them.
Ans: constants are typically written in all uppercase letters with underscores separating words. However, Python does not enforce this, so constants are not truly immutable. You can still override.
6.Write a program that calculates the area of a circle using the constant PI and a variable radius. Print the area.
Ans: ** is Squared
7.Define constants for the length and width of a rectangle. Calculate and print the area.
8.Define a constant for Ο (pi) and a variable for the radius. Calculate and print the circumference of the circle.
TASK 1: Python β Print exercises
1.How do you print the string βHello, world!β to the screen?
Ans: Using sep and end Parameters is preferred way.
2.How do you print the value of a variable name which is set to βSyed Jaferβ or Your name?
Ans: Note the Variable is case sensitive
3.How do you print the variables name, age, and city with labels βName:β, βAge:β, and βCity:β?
Ans: Using keyword sep="," brings in , between the Variable Name and Value itself, so avoid
4.How do you use an f-string to print name, age, and city in the format βName: β¦, Age: β¦, City: β¦β?
Ans: To insert variables directly into the string used f-string
Also, you can assign values to multiple variables in a single line as seen in 1st line
5.How do you concatenate and print the strings greeting (βHelloβ) and target (βworldβ) with a space between them?
Ans: + is used to concat the items
6.How do you print three lines of text with the strings βLine1β, βLine2β, and βLine3β on separate lines?****
7.How do you print the string He said, "Hello, world!" including the double quotes?
Ans: To print quotes inside a string, you can use either single or double quotes to enclose the string and the other type of quotes inside it.
8.How do you print the string C:\Users\Name without escaping the backslashes?
Ans: you can also use a literal backslash "\" when using Concat or Try with 'r' to treat backslashes as literal characters
9.How do you print the result of the expression 5 + 3?
10.How do you print the strings βHelloβ and βworldβ separated by a hyphen -?
11.How do you print the string βHelloβ followed by a space, and then print βworld!β on the same line?
12.How do you print the value of a boolean variable is_active which is set to True?
13.How do you print the string βHello β three times in a row?
14.How do you print the sentence The temperature is 22.5 degrees Celsius. using the variable temperature?
15.How do you print name, age, and city using the .format() method in the format βName: β¦, Age: β¦, City: β¦β?
16.How do you print the value of pi (3.14159) rounded to two decimal places in the format The value of pi is approximately 3.14?
Ans: pi is the variable & .2f formats it as a floating-point number with 2 digits after the decimal
17.How do you print the words βleftβ and βrightβ with βleftβ left-aligned and βrightβ right-aligned within a width of 10 characters each?
PYTHON Installation (IDLE & COLLAB Execution Test)
https://www.python.org/downloads/
check the system type of yours.
The latest series will not work on win 7 or earlier
*IDLE, an integrated development environment (IDE) for Python, to write and execute Python scripts. *
*Choose File --> New File *
Type the code & Save the file
Same above can be achieved in as below as well
Using Collab (alternate way)
https://colab.research.google.com/