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

Python ROS2 Practice Problems Guide

The document outlines a 2-week plan for practicing Python with a focus on ROS2 concepts. Week 1 covers Python basics including variables, operators, control flow, loops, and functions, while Week 2 focuses on ROS2-oriented topics such as lists, dictionaries, OOP, modules, error handling, file handling, and small ROS2-like problems. Each section contains specific practice problems to enhance programming skills relevant to robotics.

Uploaded by

jeevaratnam606
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)
9 views2 pages

Python ROS2 Practice Problems Guide

The document outlines a 2-week plan for practicing Python with a focus on ROS2 concepts. Week 1 covers Python basics including variables, operators, control flow, loops, and functions, while Week 2 focuses on ROS2-oriented topics such as lists, dictionaries, OOP, modules, error handling, file handling, and small ROS2-like problems. Each section contains specific practice problems to enhance programming skills relevant to robotics.

Uploaded by

jeevaratnam606
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 Practice Problems for ROS2 (2 Weeks Plan)

■ Week 1 – Python Basics


Variables & Data Types
• 1. Store your robot name in a variable and print it.
• 2. Store robot speed and time, calculate distance = speed * time.
• 3. Take user input for robot name and greet it.
• 4. Convert a float into int and print it.
• 5. Swap two variables without using a third variable.

Operators
• 1. Check if robot battery is greater than 50%.
• 2. Write a program to calculate area of a circle.
• 3. Compare two robot speeds and print which is faster.
• 4. Use logical operators to check if battery is >20 and temperature <50.
• 5. Use modulus operator to check if a number is even or odd.

If-Else
• 1. Check if battery <20 then print 'Low battery', else 'Battery OK'.
• 2. Check if distance > 100 then print 'Too far'.
• 3. Take input temperature and print 'High' if >40.
• 4. Nested if: Check battery and temperature both before moving.
• 5. Use elif for speed: slow, medium, fast.

Loops (for, while)


• 1. Print numbers from 1 to 10 using for loop.
• 2. Print multiplication table of a given number.
• 3. Sum first n natural numbers using while loop.
• 4. Print all sensor readings in a list.
• 5. Keep reducing battery by 15 until it reaches 0.

Functions
• 1. Write a function to calculate distance given speed and time.
• 2. Function to check if a number is prime.
• 3. Function to move robot forward with distance and speed.
• 4. Function to calculate factorial of a number.
• 5. Function with default argument speed=1.

■ Week 2 – ROS2 Oriented Python Concepts


Lists, Dictionaries, Tuples
• 1. Store 5 sensor readings in a list and print them.
• 2. Add a new reading to the list.
• 3. Store robot info in a dictionary {name, speed, battery}.
• 4. Access dictionary values and print.
• 5. Create a tuple of robot IDs and loop over it.

String formatting (f-strings)


• 1. Print 'Robot X moved Y meters'.
• 2. Store robot name and speed, print using f-string.
• 3. Print battery percentage with % symbol.
• 4. Format floating point to 2 decimals.
• 5. Combine multiple variables in one string.

OOP (Classes, Objects, Inheritance)


• 1. Create a Robot class with name attribute.
• 2. Add a method say_hello() to Robot class.
• 3. Create 2 robot objects and call methods.
• 4. Inherit from Robot to MobileRobot class.
• 5. Override method in child class.

Modules & Imports


• 1. Import math and calculate sqrt(25).
• 2. Import random and generate random numbers.
• 3. Write your own module with a function and import it.
• 4. Use os module to check current directory.
• 5. Import datetime and print current time.

Error Handling
• 1. Divide two numbers safely (catch ZeroDivisionError).
• 2. Catch ValueError for invalid int input.
• 3. Use try-except-finally to print message always.
• 4. Handle FileNotFoundError.
• 5. Raise your own error if battery <10.

File Handling (basic)


• 1. Create a text file and write 'Hello Robot'.
• 2. Read content from the file.
• 3. Append a new line into the file.
• 4. Count number of words in file.
• 5. Store sensor readings in a file.

Small ROS2-like problems


• 1. Simulate a publisher by printing 'Publishing data'.
• 2. Simulate a subscriber by reading from a list.
• 3. Write a function that simulates sending a message.
• 4. Create 2 classes (Publisher, Subscriber) and exchange messages.
• 5. Simulate robot moving until battery drains using while loop.

You might also like