x = 10
print(x)10
Open https://colab.research.google.com.
Select New Notebook.
+ CODE and + TEXT buttons in the toolbar or the buttons that show when you hover between cells.Type the following in a cell. Then hit the Play/Run button at the left side of the cell to execute it.
The statement x=10 assigns the value 10 to the variable x. The statement print(x) prints the value of x to the screen.
Exercise 1 (with Gemini): Ask Gemini to “write Python code that creates a variable named age with value 25 and prints it”
Exercise 2 (on your own): Type y = 100 then print(y) and run it.
Add a new code cell and type:
= and * are optional.x, it will not affect y.Exercise 1 (with Gemini): Ask Gemini to “write Python code that creates a variable z equal to 5 times 3 and prints it”
Exercise 2 (on your own): Type a = 10 + 20 then print(a) and run it.
print(x) and run the cell.x is not defined.x and defining y as 2*x is similar to having the value 10 in cell A1 in a spreadsheet and putting =2*A1 in cell B1.Tip: If it becomes confusing, use Runtime/Restart Session to clear Python, then use Runtime/Run Before to run all cells before a selected cell.
This is a text cell. You can double-click to edit this cell. Text cells use markdown syntax.
To learn more, see the markdown cheat sheet.
Create a new code cell. If you choose generate with AI, a prompt window will appear within the cell.
Tell Gemini what you want, and it will write code for it.
Example: Tell Gemini you want to calculate the sum of the first 100 integers. Click the play button after Gemini writes the code, and the cell will execute.
If you want advice instead of code or in addition to code, click the Gemini at the top right of the Colab window to open a chatbot.
If you ask the chatbot to do something, it will add code cells to the notebook and generate code for you.
Everyone makes errors. We need to learn to read error messages to find the line of code with the error and to get information about the error.
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) Cell In[4], line 3 1 x = 1 2 y = 4 ----> 3 z = y/(1-x) ZeroDivisionError: division by zero
If an error message isn’t clear, ask the chatbot to explain it and how to fix the error.
Exercise 1 (with Gemini): Ask Gemini to “explain what ZeroDivisionError means and how to avoid it”
Exercise 2 (on your own): Type print(10 / 2) and run it (this works correctly).