0% found this document useful (0 votes)
5 views16 pages

IntroductionToPython.ipynb

This document serves as an introduction to Python programming, covering its ease of learning, applications in various fields, and basic concepts such as variables, data types, and arithmetic operations. It provides examples of how to perform calculations using Python, including user input and the use of helper functions like print() and type(). The document also emphasizes the importance of understanding data types and statements in Python programming.

Uploaded by

dgammer995
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views16 pages

IntroductionToPython.ipynb

This document serves as an introduction to Python programming, covering its ease of learning, applications in various fields, and basic concepts such as variables, data types, and arithmetic operations. It provides examples of how to perform calculations using Python, including user input and the use of helper functions like print() and type(). The document also emphasizes the importance of understanding data types and statements in Python programming.

Uploaded by

dgammer995
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 16

{

"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Introduction to Python\n",
"\n",
"These series of notebooks are design to get you familiar with python
programming.\n",
"\n",
"<a href=\"https://python.org\">Python</a> is an easy to learn interpreted
programming language created by <a
href=\"https://en.m.wikipedia.org/Guido_van_Rossum\">Guido Van Rossum</a> and it is
one of the fastest growing languages in terms of adoption, easy to learn and large
host of library for data visualization, data analysis, data science and machine
learning which are currently hot topics in the tech industry. Python can also be
used to build websites using the <a
href=\"https://flask.palletsprojects.com/en/1.1.x/\">flask</a> or <a
href=\"https://docs.djangoproject.com/en/3.0/\">django</a> frameworks and host of
other cool things can be built using python.\n",
"<p></p>\n",
"<center>**Yea python is that cool**</center>\n",
"<img src=\"images_ui/frozen_man.jpg\" width=\"400\" alt=\"wow python is so
cool\"/>\n",
"<center>_source: pixabay.com_</center>\n",
"\n",
"Now that we have you basking in the freezing coolness of python is time to get
you **warmed up**."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"### Variables Datatypes and Statements\n",
"\n",
"Variables are simply defined as named memory locations that holds value/data.
when writting you are expected to store certain data which is then used by our
program.\n",
"\n",
"An example of a simple program will be to calculate the value of **Force**
given **mass** and **acceleration** (using the formula F=ma). To perform this
calculation mathematically we need to get the values of both mass and
acceleration.\n",
"\n",
"Assumming we have a mass of 10kg and an acceleration of 20m/s(squared)\n",
"we can perform the mathematical calculation as follows\n",
"\n",
"Force = 10x20\n",
"Force = 200N\n",
"\n",
"In programming however we have to store our values in variables which we can
refer to when performing any calculation as shown below.\n",
"\n",
"mass = 10 assigning the value of **10** to the variable **mass**\n",
"acceleration = 20 assigning the value of **20** to the variable
**acceleration**"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter mass55\n",
"Enter acceleration10\n"
]
},
{
"data": {
"text/plain": [
"550"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mass = int(input(\"Enter mass\"))\n",
"acceleration = int(input(\"Enter acceleration\"))\n",
"\n",
"force = mass*acceleration #assigning the value of the product of mass and
acceleration to the force variable\n",
"force"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"750"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"\n",
"force = 15*50 #assigning the value of the product of mass and acceleration to
the force variable\n",
"force"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"9%2"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"9//2"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4.5"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"9/2\n",
"\n",
"(a*x)+(c/b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Arithmetic operators\n",
"Arithmetic operators represent mathematical symbols used for performing
mathematical operatiions\n",
"\n",
"1. **+** Addition operator\n",
"2. **-** Subtraction operator\n",
"3. **/** Division operator\n",
"4. **\\*** Multiplication operator\n",
"5. **%** Modulus operator\n",
"6. **//** floor division operator (rounds down to the nearest whole number)\
n",
"7. **()** Bracket Operator (calculations in brackets are done first)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Datatypes\n",
"variables common in different forms called datatypes and this defines the type
of data that can be stored within each variable.\n",
"\n",
"**Common datatypes are:**\n",
"* Integers(whole numbers)\n",
"* Floating-point numbers (fractions)\n",
"* Boolean (True or False)\n",
"* String (Alpha-numeric data)\n",
"\n",
"Datatypes in most languages have to be stated at the point of variable
declaration, this is different in python as the interpreter assigns the proper
datatype for our variables.\n",
"\n",
"We can check a variables datatype by using the **type(variable)** helper
function"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"bool"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"isLoggedIn = False\n",
"type(isLoggedIn)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'float'>\n",
"<class 'int'>\n",
"Datatype for distance is: <class 'float'>\n",
"Datatype for time is: <class 'int'>\n"
]
}
],
"source": [
"distance = 13.5\n",
"time_taken = 2\n",
"\n",
"print(type(12.0)) #Checking the type of the value 12.0\n",
"print(type(12)) #Checking the type of the value 12\n",
"print(\"Datatype for distance is: \",type(distance))\n",
"print(\"Datatype for time is: \",type(time_taken))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice that **12.0** and **12** have different datatypes, this is because the
decimal point denotes a floating point number and the python interpreter is smart
enough to figure this out\n",
"\n",
"so far we have used two helper functions **print()** and **type()** while we
will discuss functions later, to clear the air the print() function is used to
output information as seen above while the type() function shows us what the
datatype of the variable or value is"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Statements\n",
"\n",
"A statement is an instruction that a python interpreter executes\n",
"**distance = 13.5** this is an assignment statement, assigning the the value
13.5 to the variable distance.\n",
"\n",
"There are several types of statements which will be discussed later, they are:
\n",
"1. conditional statements\n",
"2. while statements\n",
"3. for statement\n",
"etc."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Enough Talk\n",
"\n",
"Lets write some code.\n",
"\n",
"1. Assuming given the radius of a circle as 7, how do we calculate the
circumference of the circle? Formula (2\\*pi\\*radius) where pi is 22/7\n",
">Looking at this question we quickly notice we need three variables one to
hold pi, another to hold radius \n",
"and lastly a circumference variable to store our result which is the product
of radius, pi and 2\n",
"\n",
"2. An object travelled a distance of 2km within a time of 30s, how do we
calculate the velocity of this object? Formula (distance/time)\n",
">This question requires us to have three variables distance, time and velocity
to hold the result of dividing distance by time"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"44.0\n"
]
}
],
"source": [
"pi = 22/7 #assigning the value 22/7 to pi variable\n",
"radius = 7 #assigning the value of 7 to radius variable\n",
"circumference = 2*pi*radius #assigning the result of out calculation to the
circumference variable\n",
"\n",
"print(circumference)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice the number 2 wasn't stored in a variable, this is because it will be
pointless to store a value that doesn't change as a variable in this case, but
there cases where it required."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.06666666666666667"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"distance = 2\n",
"time = 30\n",
"\n",
"velocity = distance/time\n",
"velocity"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#Note the differences between this three types of division\n",
"print(13/2)\n",
"print(13//2)\n",
"print(13%2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So far the values of our variables are hardcoded meaning we assign them right
in the code and this won't work in a real life environment since the data will
**varry** in different scenarios.\n",
"This is where we introduced another helper function **input()** which collects
user input, used as follows\n",
"\n",
">variable = input(\"Message the User\")\n",
"\n",
"So lets change our second example to accept user inputs"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the value of Radius7\n"
]
},
{
"ename": "TypeError",
"evalue": "can't multiply sequence by non-int of type 'float'",
"output_type": "error",
"traceback": [
"\u001b[1;31m-----------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-18-140875e68a84>\u001b[0m in \
u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \
u001b[0mpi\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m22\u001b[0m\u001b[1;33m/\
u001b[0m\u001b[1;36m7\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\
u001b[0m \u001b[0mradius\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minput\u001b[0m\
u001b[1;33m(\u001b[0m\u001b[1;34m\"Enter the value of Radius\"\u001b[0m\
u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\
u001b[1;33m \u001b[0mcircumference\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m2\
u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mpi\u001b[0m\u001b[1;33m*\u001b[0m\
u001b[0mradius\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m
4\u001b[0m \u001b[0mcircumference\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mTypeError\u001b[0m: can't multiply sequence by non-int of type
'float'"
]
}
],
"source": [
"pi = 22/7\n",
"radius = input(\"Enter the value of Radius\")\n",
"circumference = 2*pi*radius\n",
"circumference"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So if you run code above you should get the following error\n",
">TypeError: can't multiply sequence by non-int of type 'float'\n",
"\n",
"This is so because the input() function collects its data inform of string you
can check that for fun using the you guessed it type() function.\n",
"\n",
"How do we solve this issue, surely there has to be some way to convert data
from text to float or integer, luckily we can. Converting the number can be done
like so\n",
">radius = int(input(\"Enter the value of Radius\")) #converting to integer\n",
"\n",
">radius = float(input(\"Enter the value of Radius\")) #converting to float"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the value of Radius9\n",
"The Circumference is: 56.57142857142857\n"
]
}
],
"source": [
"#The correct solution will be this\n",
"pi = 22/7\n",
"radius = float(input(\"Enter the value of Radius\")) #converting our string
input data to float\n",
"circumference = 2*pi*radius\n",
"print(\"The Circumference is: \",circumference)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Logic/Relational operators\n",
"\n",
"\\> Greater than\n",
"\n",
"< Less than\n",
"\n",
"and (makes sure left and right statements are true)\n",
"\n",
"not\n",
"\n",
"or (either left and right statements are true)\n",
"\n",
"\\>= greater than or equal to\n",
"\n",
"<= less than or equal to"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Conditional Statements\n",
"Decision making is required when we want to execute a code only if a certain
condition is satisfied.\n",
"\n",
"The \n",
"> if…elif…else \n",
"\n",
"statement is used in Python for decision making."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"height = float(input(\"Enter your height: \"))\n",
"age = int(input(\"Enter your age: \"))\n",
"\n",
"if(height > 170):\n",
" print('welcome')\n",
"elif(age>17):\n",
" print('welcome')\n",
"else:\n",
" print('you are not qualified')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"anotherHeight = float(input(\"Enter your height: \"))\n",
"anotherAge = int(input(\"Enter your age: \"))\n",
"\n",
"if(anotherHeight > 170 or anotherAge >17):\n",
" print('welcome')\n",
"else:\n",
" print('you are not qualified')"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## For Loop\n",
"\n",
"The for loop in Python is used to iterate over a sequence (list, tuple,
string) or other iterable objects. Iterating over a sequence is called traversal.\
n",
"\n",
">for val in sequence:\n",
"\n",
"Here, val is the variable that takes the value of the item inside the sequence
on each iteration.\n",
"\n",
"Loop continues until we reach the last item in the sequence. The body of for
loop is separated from the rest of the code using indentation."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n"
]
}
],
"source": [
"#for loop with a single parameter for range\n",
"for i in range(4):\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n"
]
}
],
"source": [
"#for loop with a two parameters for range (start_value, end_value)\n",
"for i in range(1, 4):\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"3\n",
"6\n",
"9\n",
"12\n",
"15\n",
"18\n"
]
}
],
"source": [
"#for loop with a two parameter for range (start_value, end_value, step_size)\
n",
"for i in range(0, 20, 3):\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Purpose of a loop"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Donald', 'Dorcas', 'Sam', 'Igwe']\n",
"I am looking for Dorcas!\n",
"I have been looking for you\n"
]
}
],
"source": [
"#List of names\n",
"names = [] #empty names list\n",
"names.append(\"Donald\")#index 0\n",
"names.append(\"Dorcas\")#index 1\n",
"names.append(\"Joseph\")#index 2\n",
"names.append(\"Igwe\")\n",
"names[2] = \"Sam\"\n",
"\n",
"print(names)\n",
"#names.pop(\"Jon\")\n",
"\n",
"for name in names:\n",
" if(name == \"Dorcas\"):\n",
" print(\"I have been looking for you\")\n",
" break; #terminates the loop\n",
" else:\n",
" print(\"I am looking for Dorcas!\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6 + 0\n",
"5 + 6\n",
"3 + 11\n",
"8 + 14\n",
"4 + 22\n",
"2 + 26\n",
"4 + 28\n",
"11 + 32\n",
"8 + 43\n",
"The sum is 51\n"
]
}
],
"source": [
"# List of numbers\n",
"numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]\n",
"numbers.append(8)\n",
"numbers.pop(6)\n",
"# variable to store the sum\n",
"sum = 0\n",
"\n",
"# iterate over the list\n",
"for val in numbers:\n",
" print(val,\" + \",sum)\n",
" sum = sum+val\n",
" \n",
"\n",
"print(\"The sum is\", sum)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Functions\n",
"\n",
"In Python, function is a group of related statements that perform a specific
task.\n",
"\n",
"Functions help break our program into smaller and modular chunks. As our
program grows larger and larger, functions make it more organized and manageable.\
n",
"\n",
"Furthermore, it avoids repetition and makes code reusable.\n",
"\n",
"\n",
"def nameOfTheFunction():"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello functions\n"
]
},
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def myFunction():\n",
" print(\"Hello functions\")\n",
" return 0\n",
"myFunction()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def checkUserHeight(height):\n",
" #isTallEnough = False\n",
" if(height > 170):\n",
" print('welcome')\n",
" #isTallEnough = True\n",
" else:\n",
" print('you are not qualified')\n",
" return 0"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter your height: 67\n",
"you are not qualified\n"
]
},
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"myHeight = float(input(\"Enter your height: \"))\n",
"\n",
"checkUserHeight(myHeight)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def calcVelocity(dist, time):\n",
" velocity = dist/time\n",
" return velocity"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def calForce(mass, acceleration):\n",
" force = mass*acceleration\n",
" return force"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter Distance Travelled: 56\n",
"Enter Time taken: 6\n",
"9.333333333333334\n"
]
}
],
"source": [
"dist = float(input(\"Enter Distance Travelled: \"))\n",
"time = float(input(\"Enter Time taken: \"))\n",
"\n",
"velocity = calcVelocity(dist,time)\n",
"\n",
"print(velocity)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

You might also like