|
68 | 68 | "\n", |
69 | 69 | "You can [create](https://aistudio.google.com/app/apikey) your API key using Google AI Studio with a single click. \n", |
70 | 70 | "\n", |
71 | | - "Remember to treat your API key like a password. Do not accidentally save it in a notebook or source file you later commit to GitHub. This notebook shows you two ways you can securely store your API key.\n", |
| 71 | + "Remember to treat your API key like a password. Don't accidentally save it in a notebook or source file you later commit to GitHub. This notebook shows you two ways you can securely store your API key.\n", |
72 | 72 | "\n", |
73 | | - "* If you are using Google Colab, it is recommended to store your key in Colab Secrets.\n", |
| 73 | + "* If you're using Google Colab, it's recommended to store your key in Colab Secrets.\n", |
74 | 74 | "\n", |
75 | | - "* If you are using a different development environment (or calling the Gemini API through `cURL` in your terminal), it is recommended to store your key in an [environment variable](https://en.wikipedia.org/wiki/Environment_variable).\n", |
| 75 | + "* If you're using a different development environment (or calling the Gemini API through `cURL` in your terminal), it's recommended to store your key in an [environment variable](https://en.wikipedia.org/wiki/Environment_variable).\n", |
76 | 76 | "\n", |
77 | 77 | "Let's start with Colab Secrets." |
78 | 78 | ] |
|
124 | 124 | "source": [ |
125 | 125 | "## Configure the SDK with your API key\n", |
126 | 126 | "\n", |
127 | | - "You create a client using your API key, but instead of pasting your key into the notebook, you will read it from Colab Secrets thanks to `userdata`." |
| 127 | + "You create a client using your API key, but instead of pasting your key into the notebook, you'll read it from Colab Secrets thanks to `userdata`." |
128 | 128 | ] |
129 | 129 | }, |
130 | 130 | { |
|
168 | 168 | "id": "tr7oAO6-nMsE" |
169 | 169 | }, |
170 | 170 | "source": [ |
171 | | - "And that's it! Now you are ready to call the Gemini API." |
| 171 | + "And that's it! Now you're ready to call the Gemini API." |
172 | 172 | ] |
173 | 173 | }, |
174 | 174 | { |
|
180 | 180 | "outputs": [ |
181 | 181 | { |
182 | 182 | "data": { |
183 | | - "text/markdown": "Okay, Python provides two main ways to sort a list:\n\n1. **`list.sort()`:** This method sorts the list *in-place*. It modifies the original list and returns `None`.\n2. **`sorted(list)`:** This is a built-in function that returns a *new* sorted list, leaving the original list unchanged.\n\nHere are examples of both:\n\n**1. Using `list.sort()` (Sorts In-Place)**\n\n```python\n# Original list\nmy_list = [3, 1, 4, 1, 5, 9, 2, 6]\n\nprint(f\"Original list: {my_list}\")\n\n# Sort the list in-place (ascending order by default)\nmy_list.sort()\n\nprint(f\"List after my_list.sort(): {my_list}\")\n\n# You can also sort in descending order\nmy_list.sort(reverse=True)\n\nprint(f\"List after my_list.sort(reverse=True): {my_list}\")\n\n# Important: my_list.sort() returns None\nresult = my_list.sort()\nprint(f\"Result of my_list.sort(): {result}\") # This will print None\nprint(f\"List after calling sort() again: {my_list}\") # List is sorted again (ascending this time)\n```\n\n**2. Using `sorted(list)` (Returns a New Sorted List)**\n\n```python\n# Original list\nanother_list = [9, 5, 2, 7, 1, 8, 3, 6]\n\nprint(f\"\\nOriginal list for sorted(): {another_list}\")\n\n# Get a new sorted list (ascending order)\nnew_sorted_list_asc = sorted(another_list)\n\nprint(f\"Original list after sorted(): {another_list}\") # Original list is unchanged\nprint(f\"New sorted list (ascending): {new_sorted_list_asc}\")\n\n# Get a new sorted list (descending order)\nnew_sorted_list_desc = sorted(another_list, reverse=True)\n\nprint(f\"New sorted list (descending): {new_sorted_list_desc}\")\n\n# sorted() also works on other iterables, not just lists\nmy_tuple = (5, 2, 8, 1)\nsorted_tuple_as_list = sorted(my_tuple)\nprint(f\"\\nOriginal tuple: {my_tuple}\")\nprint(f\"Sorted tuple (as list): {sorted_tuple_as_list}\")\n```\n\n**Which one to use?**\n\n* Use `list.sort()` if you don't need the original order of the list and want to modify it directly (often slightly more efficient for lists as it doesn't create a new list).\n* Use `sorted(list)` if you need to keep the original list unchanged or if you are sorting an iterable that is not a list (like a tuple, string, or set).\n\nBoth methods support the `key` argument for custom sorting criteria (e.g., sorting a list of strings by their length, or a list of objects by one of their attributes).", |
| 183 | + "text/markdown": [ |
| 184 | + "Okay, Python provides two main ways to sort a list:\n", |
| 185 | + "\n", |
| 186 | + "1. **`list.sort()`:** This method sorts the list *in-place*. It modifies the original list and returns `None`.\n", |
| 187 | + "2. **`sorted(list)`:** This is a built-in function that returns a *new* sorted list, leaving the original list unchanged.\n", |
| 188 | + "\n", |
| 189 | + "Here are examples of both:\n", |
| 190 | + "\n", |
| 191 | + "**1. Using `list.sort()` (Sorts In-Place)**\n", |
| 192 | + "\n", |
| 193 | + "```python\n", |
| 194 | + "# Original list\n", |
| 195 | + "my_list = [3, 1, 4, 1, 5, 9, 2, 6]\n", |
| 196 | + "\n", |
| 197 | + "print(f\"Original list: {my_list}\")\n", |
| 198 | + "\n", |
| 199 | + "# Sort the list in-place (ascending order by default)\n", |
| 200 | + "my_list.sort()\n", |
| 201 | + "\n", |
| 202 | + "print(f\"List after my_list.sort(): {my_list}\")\n", |
| 203 | + "\n", |
| 204 | + "# You can also sort in descending order\n", |
| 205 | + "my_list.sort(reverse=True)\n", |
| 206 | + "\n", |
| 207 | + "print(f\"List after my_list.sort(reverse=True): {my_list}\")\n", |
| 208 | + "\n", |
| 209 | + "# Important: my_list.sort() returns None\n", |
| 210 | + "result = my_list.sort()\n", |
| 211 | + "print(f\"Result of my_list.sort(): {result}\") # This will print None\n", |
| 212 | + "print(f\"List after calling sort() again: {my_list}\") # List is sorted again (ascending this time)\n", |
| 213 | + "```\n", |
| 214 | + "\n", |
| 215 | + "**2. Using `sorted(list)` (Returns a New Sorted List)**\n", |
| 216 | + "\n", |
| 217 | + "```python\n", |
| 218 | + "# Original list\n", |
| 219 | + "another_list = [9, 5, 2, 7, 1, 8, 3, 6]\n", |
| 220 | + "\n", |
| 221 | + "print(f\"\\nOriginal list for sorted(): {another_list}\")\n", |
| 222 | + "\n", |
| 223 | + "# Get a new sorted list (ascending order)\n", |
| 224 | + "new_sorted_list_asc = sorted(another_list)\n", |
| 225 | + "\n", |
| 226 | + "print(f\"Original list after sorted(): {another_list}\") # Original list is unchanged\n", |
| 227 | + "print(f\"New sorted list (ascending): {new_sorted_list_asc}\")\n", |
| 228 | + "\n", |
| 229 | + "# Get a new sorted list (descending order)\n", |
| 230 | + "new_sorted_list_desc = sorted(another_list, reverse=True)\n", |
| 231 | + "\n", |
| 232 | + "print(f\"New sorted list (descending): {new_sorted_list_desc}\")\n", |
| 233 | + "\n", |
| 234 | + "# sorted() also works on other iterables, not just lists\n", |
| 235 | + "my_tuple = (5, 2, 8, 1)\n", |
| 236 | + "sorted_tuple_as_list = sorted(my_tuple)\n", |
| 237 | + "print(f\"\\nOriginal tuple: {my_tuple}\")\n", |
| 238 | + "print(f\"Sorted tuple (as list): {sorted_tuple_as_list}\")\n", |
| 239 | + "```\n", |
| 240 | + "\n", |
| 241 | + "**Which one to use?**\n", |
| 242 | + "\n", |
| 243 | + "* Use `list.sort()` if you don't need the original order of the list and want to modify it directly (often slightly more efficient for lists as it doesn't create a new list).\n", |
| 244 | + "* Use `sorted(list)` if you need to keep the original list unchanged or if you are sorting an iterable that is not a list (like a tuple, string, or set).\n", |
| 245 | + "\n", |
| 246 | + "Both methods support the `key` argument for custom sorting criteria (e.g., sorting a list of strings by their length, or a list of objects by one of their attributes)." |
| 247 | + ], |
184 | 248 | "text/plain": [ |
185 | 249 | "<IPython.core.display.Markdown object>" |
186 | 250 | ] |
|
215 | 279 | "id": "gZDX51Y27pN4" |
216 | 280 | }, |
217 | 281 | "source": [ |
218 | | - "If you are using a different development environment (or calling the Gemini API through `cURL` in your terminal), it is recommended to store your key in an environment variable.\n", |
| 282 | + "If you're using a different development environment (or calling the Gemini API through `cURL` in your terminal), it's recommended to store your key in an environment variable.\n", |
219 | 283 | "\n", |
220 | 284 | "To store your key in an environment variable, open your terminal and run:\n", |
221 | 285 | "\n", |
222 | 286 | "```export GOOGLE_API_KEY=\"YOUR_API_KEY\"```\n", |
223 | 287 | "\n", |
224 | | - "If you are using Python, you can add these two lines to your notebook to read the key:\n", |
| 288 | + "If you're using Python, you can add these two lines to your notebook to read the key:\n", |
225 | 289 | "\n", |
226 | 290 | "```\n", |
227 | 291 | "import os\n", |
228 | 292 | "client = genai.Client(api_key=os.environ['GOOGLE_API_KEY'])\n", |
229 | 293 | "```\n", |
230 | 294 | "\n", |
231 | | - "Alternatively, if it is not provided explicitly, the client will look for the API key.\n", |
| 295 | + "Alternatively, if it isn't provided explicitly, the client will look for the API key.\n", |
232 | 296 | "\n", |
233 | 297 | "```\n", |
234 | 298 | "client = genai.Client()\n", |
235 | 299 | "```\n", |
236 | 300 | "\n", |
237 | | - "Or, if you are calling the API through your terminal using `cURL`, you can copy and paste this code to read your key from the environment variable.\n", |
| 301 | + "Or, if you're calling the API through your terminal using `cURL`, you can copy and paste this code to read your key from the environment variable.\n", |
238 | 302 | "\n", |
239 | 303 | "```\n", |
240 | 304 | "curl \"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GOOGLE_API_KEY\" \\\n", |
|
258 | 322 | "source": [ |
259 | 323 | "## Learning more\n", |
260 | 324 | "\n", |
261 | | - "Now that you know how to manage your API key, you have everything to [get started](./Get_started.ipynb) with Gemini. Check all the [quickstart guides](https://github.com/google-gemini/cookbook/tree/main/quickstarts) from the Cookbook, and in particular the [Get started](./Get_started.ipynb) one." |
| 325 | + "Now that you know how to manage your API key, you've everything to [get started](./Get_started.ipynb) with Gemini. Check all the [quickstart guides](https://github.com/google-gemini/cookbook/tree/main/quickstarts) from the Cookbook, and in particular the [Get started](./Get_started.ipynb) one." |
262 | 326 | ] |
263 | 327 | } |
264 | 328 | ], |
|
0 commit comments