Skip to content

Instantly share code, notes, and snippets.

@craine
Created April 13, 2020 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craine/bdffa0a1af6ff20c298a76ff070c4204 to your computer and use it in GitHub Desktop.
Save craine/bdffa0a1af6ff20c298a76ff070c4204 to your computer and use it in GitHub Desktop.
how to create python class variables.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "how to create python class variables.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyNnrkjLzA4YPHB8MHlBmZTU",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/craine/bdffa0a1af6ff20c298a76ff070c4204/how-to-create-python-class-variables.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "bIQCxqZz6Dal",
"colab_type": "code",
"colab": {}
},
"source": [
"class Car: \n",
" def __init__(self, brand, year): \n",
" #Initialize brand and year attributes\n",
" self.brand = brand \n",
" self.year = year \n",
" \n",
" def forward(self): \n",
" #car will go forward\n",
" print(f\"{self.brand} is going forward.\") \n",
" \n",
" def reverse(self): \n",
" #car goes in reverse\n",
" print(f\"{self.brand} went in reverse\")"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "R6Zwo7Ch6Ko5",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 51
},
"outputId": "181d7ea3-82e0-4b3b-c126-b2e4a131ce2f"
},
"source": [
"my_car = Car('Tesla', 2018)\n",
"\n",
"print(f\"My car is a {my_car.brand}.\")\n",
"print(f\"My car is a {my_car.year} {my_car.brand}.\")"
],
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": [
"My car is a Tesla.\n",
"My car is a 2018 Tesla.\n"
],
"name": "stdout"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment