Introduction to Python Programming using
Google Colab
Objective : What I want you to learn?
Python programing using the Google Colab for writing simple codes involving arithmetic operations.
Appreciate the highlight the features of Google Colab
Rationale : Why this is useful to you?
Writing the python codes for solve engineering problems will help to analyze the variables in the engineering problems and make us better understand the concepts.
Learning Outcomes : What I want you to do after completing this topic?
Write simple python programs in the Google Colab Notebook
Appreciate the significance of python programing using Google Colab
Contents
Introduction to Python Programming Language
Introduction and Features of Google Colab
Opening, saving and sharing Colab Notebook
Basic Python syntax and comments
Documenting Python code using Markdown syntax
Introduction to Python Programming Language
Python is a general-purpose high-level, remarkably powerful dynamic programming language that is used in a wide variety of application domains. Python supports multiple programming paradigms, including object-oriented, imperative and functional programming styles. It was created by Guido van Rossum during 1985- 1990. Python source code is available under the GNU General Public License (GPL). Its one of the most used programing language among the developers worldwide.
Python, as a programming language is easy and simple to learn. Python cuts development time in half with its simple to read syntax and easy compilation feature. Also, it has plenty of libraries that support data analysis, manipulation and visualization.
The popularity of Python programming is steadily rising and so are Python career opportunities
Introduction and Features of Google Colab
Google Colab, or Colaboratory, is a free Jupyter Notebook environment that runs in the cloud. Colab, or ‘Colaboratory’, allows you to write and execute Python in your browser, with
Zero configuration required
Access to GPUs free of charge
Easy sharing
Features of Google Colab
Write and execute code in Python
Document your code that supports mathematical equations
Create/Upload/Share notebooks
Import/Save notebooks from/to Google Drive
Import/Publish notebooks from GitHub
Import external datasets e.g. from Kaggle
Integrate PyTorch, TensorFlow, Keras, OpenCV
Free Cloud service with free GPU
Opening and Wroking with a Colab Notebook
Colab implicitly uses Google Drive for storing your notebooks,
Login into your Google Drive account
Go to the Google Colab website: https://colab.research.google.com/.
Click the File menu and select New Notebook.
You can choose to create a blank notebook or a notebook with a template.
Once you have created a notebook, you can start writing code.
Rename the notebook with the suitable name
Notebook is saved as .ipynb format
In the Code Cell of the Google Colab, Type the following code,
print("Python Programming for Engineers")
Click on the Run Cell Icon to Run the Line of code, within microseconds code gets executed and the output is displayed in the Output Cell.
Basic Python Syntax and Comments
Python uses indentation to group statements together. All statements within a block of code must be indented at the same level.
Python uses keywords to define different parts of a program, such as functions, loops, and conditional statements.
Python uses variables to store data. Variables are declared using the = operator.
Python uses operators to perform operations on data. Some common operators include +, -, *, and /.
Writing a Simple Python Code and Running
Basic Arithmetic Operation of Numbers
# Code for Arithemetic Operations of Numbers
num1 = 20
num2 = 5
# Addition
sum = num1 + num2
print(sum)
# Subtraction
difference = num1 - num2
print(difference)
# Multiplication
product = num1 * num2
print(product)
# Division
quotient = num1 / num2
print(quotient)
# Exponentiation power = num1 ** num2
print(power)
# Modulos
remainder = num1 % num2
print(remainder)
Comments in Python
Comments are used to make code more readable and understandable. They are ignored by the Python interpreter, but they can be helpful for explaining what your code is doing.
Single-line comments start with a hash (#) and extend to the end of the line.
Multi-line comments start with three double quotes (""") and end with three double quotes (""").
Some of keyboard shortcuts in Google Colab:
Ctrl + Enter: Run the selected cell.
Shift + Enter: Run the selected cell and move to the next cell.
Alt + Enter: Run the selected cell and insert a new cell below it.
Ctrl + M: Toggle between edit mode and command mode.
Ctrl + S: Save the notebook.
Documenting Python code using Markdown
Code Cell supports Python syntax, and we can use Python comments to describe the code.
Colaboratory also supports simple text based documentation using Text Cells, which supports Markdown syntax and LaTex - a language for mathematical representations using which we can document mathematical equations.
Summary and Practice Assignment
Summary
Introduction to Python programming language and Google Colab
Opening, working, saving and sharing Google Colab Notebook
Writing simple codes using Python programming language
Using Markdown and LaTex for documenting the code
Practice Assignment
Write a Python codes for
Converting the Temperature from Celsius to Fahrenheit Scale and Vice-Versa
Calculating the resistance of a wire provided with resistivity, cross-sectional area and length
Calculating the voltage drop across a resistor
Calculating the force required to accelerate a mass
Calculating the kinetic energy of a moving object
Calculating the work done by a force
Calculating the potential energy of an object in a gravitational field
Calculating the stress and strain in a rod subjected to an axial force
Calculating the Circumference and Area of Two dimensional shapes such as Circle, Square, Rectangle and Rhombus.
Calculating the Surface Area and Volume of three dimensional shapes such as Cylinder, Sphere, Cone and Torus.