Computer Science
Grade 7
20 min
3. Introduction to Robotics Programming Languages: Python and ROS
An introduction to robotics programming languages like Python and the Robot Operating System (ROS).
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define and use lists to store collections of data, like sensor readings.
Access and modify specific elements in a list using their index.
Create and use dictionaries to store key-value pairs, like robot settings.
Write their own Python functions that accept parameters to perform specific tasks.
Use the `return` keyword to get a value back from a function.
Combine lists, dictionaries, and functions to model a simple robot's state and behavior.
How does a robot remember a whole list of instructions or all the parts it has? Let's find out how Python gives robots a memory! 🤖ðŸ§
In this lesson, we'll level up our Python skills by learning about lists, dictionaries, and functions. These tools are essential for writing smarter, more organized cod...
2
Key Concepts & Vocabulary
TermDefinitionExample
ListAn ordered collection of items, stored in a single variable. Lists are mutable, meaning you can change their contents.A list of distance sensor readings: `distances = [10, 12, 15, 9, 11]`
IndexThe position of an item in a list. Python indexing starts at 0.In the list `distances = [10, 12, 15]`, the number `10` is at index `0`, and `15` is at index `2`.
DictionaryA collection of key-value pairs. Instead of an index, you use a unique 'key' to look up a 'value'.A dictionary for robot motor speeds: `motor_speeds = {'left': 100, 'right': 95}`. Here, 'left' is a key and 100 is its value.
FunctionA named block of reusable code that performs a specific action. Functions help organize code and avoid repetition.A function t...
3
Core Syntax & Patterns
Function Definition Syntax
def function_name(parameter1, parameter2):
# code to execute
return result
Use the `def` keyword to create a new function. Name it, list any parameters it needs in parentheses, and end the line with a colon. The code inside the function must be indented. Use `return` to send a value back.
Accessing List Elements
my_list[index]
Use the list's variable name followed by square brackets `[]` containing the index of the element you want. Remember that the first element is at index 0.
Accessing Dictionary Values
my_dictionary['key']
Use the dictionary's variable name followed by square brackets `[]` containing the key (as a string) whose value you want to retrieve.
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
A programmer modified the `is_obstacle_close` function. The incorrect code is: `def is_obstacle_close(sensor_readings): for reading in sensor_readings: if reading > 20: return True; return False`. What is the error in its logic?
A.The function should return a number, not True or False.
B.It will return `True` for distant objects and `False` for close ones, the opposite of what is intended.
C.It will always cause a syntax error because of the semicolon.
D.It will always return `False` because the `return False` is not indented correctly.
Challenging
A robot's program first checks for obstacles, then updates its status. Given `readings = [50, 40, 12, 60]` and `status = {'mode': 'patrol', 'battery': 75}`. The program runs `obstacle = is_obstacle_close(readings)`. If `obstacle` is `True`, it sets `status['mode'] = 'avoid'`. What is the final value of `status['mode']`?
A.'patrol'
B.75
C.'avoid'
D.True
Challenging
You need a function that checks if a robot's name is 'Rover1'. Given `robot_status = {'name': 'Rover1', 'battery': 85}`. Which function correctly implements this check?
A.`def check_name(status): return status['name']`
B.`def check_name(status): if name == 'Rover1': return True`
C.`def check_name(status): return 'Rover1'`
D.`def check_name(status): if status['name'] == 'Rover1': return True else: return False`
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free