BAHRIA UNIVERSITY KARACHI CAMPUS
Department of Electrical Engineering
ARTIFICIAL INTELLIGENCE
LAB EXPERIMENT # 1
Getting started with Anaconda (Jupyter notebook) & Introduction to python
OBJECTIVE:-
• Learn to install and navigate the Anaconda environment and Jupyter Notebook.
• Basic Introduction of Python
• Implementation of basic commands of Python within Jupyter Notebook for hands-on learning.
What is ANACONDA?
Anaconda is a distribution of the Python and R programming languages for scientific computing (data
science, machine learning applications, large-scale data processing, predictive analytics, etc.), that aims to
simplify package management and deployment.
Languages used: Python
Software genre: Programming language
Source of definition:
https://en.wikipedia.org/wiki/Anaconda_(Python_distribution)#:~:text=Anaconda%20is%20a%20distribut
ion%20of,simplify%20package%20management%20and%20deployment.
How to start and set up the project in ANACONDA?
Install Anaconda, as any other software, by one click installation after downloading from the following link
https://www.anaconda.com/products/individual
Start Anaconda :
● Start Anaconda from Start Menue and it will look like this:
● Once it opens, the interface looks like this:
● As we are going to use, for
now, jupyter notebook:
● We are supposed to open it in specific directory
● Go to start menu, type anaconda prompt
● Open it
● Type command to navigate to the directory you want to create a project
● Use cd command to go to the specific folder where your project will be created
● After jumping to the specific folder (e.g. “AI”), type “Jupyter notebook”. It will start Jupyter Notebook
and prompt for selecting the browser of your choice (Chrome is recommended).
● Following tab appears once the browser application is chosen:
● Create new project by clicking on New, and select Python 3:
● You are now ready to code in the following window:
2. Spyder
Spyder, the Scientific Python Development Environment, is a free integrated development environment
(IDE) that is included with Anaconda. It includes editing, interactive testing, debugging, and introspection
features. By starting Spyder, following interface appears:
● Example ode in Code Panel
● To run the code, select it and Play or press Shift+Enter. The output will be displayed as:
● Along the output console, displayed is variable console which shows variables and values.
3. Google Colab
What is Google Colab?
Colaboratory, or “Colab” for short, is a product from Google Research. Colab allows anybody to write and
execute arbitrary python code through the browser, and is especially well suited to machine learning, data
analysis and education. More technically, Colab is a hosted Jupyter notebook service that requires no setup
to use, while providing free access to computing resources including GPUs.
● Go to google and searc for https://colab.research.google.com/. The Colab prompts welcome screen:
● Following is the home screen of Google Colab.
● To create new project, go to File New notebook, or press Ctrl+O to open an existing project.
● The new project file looks as following. Start coding:
● An example code looks as following:
Note: See highlighted part:
1. Name is changed from Untitled2 to Name_Changed
2. Aside from connect now, it is connected to Server
3. And cell is run and the output is shown under the code line.
4. Basic Python Program
This section explains further on how to write a python program. Read comments carefully in the given
screen shot. print() is used to output something on the console:
1. # is used for single line comment
2. To print output, print() function which accepted ‘String’ or “ String” in single or
double quotes.
3. To run individual cell, press Shift+Enter or run button
● To comment on the code, use “”” Your comments go here“””
LAB NO 1
● To retrieve input from user, use input() function
Note. Python does not need to define data type for input variable, it assigns the type based on
data provided by the user. For example, in the program below, Python assigns string type to
variable name, since user entered a string data.
1
LAB NO 1
Lab Exercises:
Provide appropriate doc string for both of the following tasks.
Exercise 1
Create a program that takes First name and Last name as input from the user and print it after
concatenating the both.
Exercise 2
Create a program that takes two numbers as input from the user and calculate the sum, product,
difference between the two inputted variables.
When you run the file hello_world.py, the ending .py indicates that the file is a Python program.
Your editor then runs the file through the Python interpreter, which reads through the program
and determines what each word in the program means. For example, when the interpreter sees
the word print, it prints to the screen whatever is inside the parentheses.
As you write your programs, your editor highlights different parts of your program in different
ways. For example, it recognizes that print is the name of a function and displays that word in
blue. It recognizes that “Hello Python world!” is not Python code and displays that phrase in
orange. This feature is called syntax highlighting and is quite useful as you start to write your
own programs.
Variables and Data Types
Variables
Let’s try using a variable in hello_world.py. Add a new line at the beginning of the file, and
modify the second line:
message = "Hello Python world!"
print(message)
When you’re using variables in Python, you need to adhere to a few rules and guidelines.
Breaking some of these rules will cause errors; other guidelines just help you write code that’s
easier to read and understand. Be sure to keep the following variable rules in mind:
• Variable names can contain only letters, numbers, and underscores. They can start with a
letter or an underscore, but not with a number. For instance, you can call a variable
message_1 but not 1_message.
2
LAB NO 1
• Spaces are not allowed in variable names, but underscores can be used to separate words in
variable names. For example, greeting_message works, but greeting message will cause
errors.
• Avoid using Python keywords and function names as variable names; that is, do not use
words that Python has reserved for a particular programmatic purpose, such as the word
print.
• Variable names should be short but descriptive. For example, name is better than n,
student_name is better than s_n, and name_length is better than length_of_persons_name.
• Be careful when using the lowercase letter l and the uppercase letter O because they could
be confused with the numbers 1 and 0.
Data types
Strings
A string is simply a series of characters. Anything inside quotes is considered a string in Python,
and you can use single or double quotes around.
your strings like this:
"This is a string."
'This is also a string.'
This flexibility allows you to use quotes and apostrophes within your strings:
Numbers
Python treats numbers in several different ways, depending on how they are being used. Let’s
first look at how Python manages integers, because they are the simplest to work with.
Integers
You can add (+), subtract (-), multiply (*), and divide (/) integers in Python.
>>> 2 + 3
5
>>> 3 - 2
1
>>> 2 * 3
6
>>> 3 / 2
1.5
In a terminal session, Python simply returns the result of the operation.
Python uses two multiplication symbols to represent exponents:
>>> 3 ** 2
9
>>> 3 ** 3
27
>>> 10 ** 6
1000000
3
LAB NO 1
Python supports the order of operations too, so you can use multiple operations in one
expression. You can also use parentheses to modify the order of operations so Python can
evaluate your expression in the order you specify. For example:
>>> 2 + 3*4
14
>>> (2 + 3) * 4
20
Floats
Python calls any number with a decimal point a float. This term is used in most programming
languages, and it refers to the fact that a decimal point can appear at any position in a number.
Every programming language must
be carefully designed to properly manage decimal numbers so numbers behave appropriately no
matter where the decimal point appears.For the most part, you can use decimals without
worrying about how they behave. Simply enter the numbers you want to use, and Python will
most likely do what you expect:
>>> 0.1 + 0.1
0.2
>>> 0.2 + 0.2
0.4
>>> 2 * 0.1
0.2
>>> 2 * 0.2
0.4
But be aware that you can sometimes get an arbitrary number of decimal places in your answer.
4
LAB NO 1
Lab Worksheet
1. Create a new notebook on Google Colab and name it as Lab 1.
2. Declare a variable and store an integer value in it. Write the command you have used.
Command Used: ____________________________
3. Find the datatype of following variables using appropriate command.
Variable Datatype
A = “Hello World! “
B = 21
C = 21.345
4. Which Command you have used to find the datatypes of above given variables.
Command Used: ____________________________
5. Run any false statement in a code cell and write the output of that statement below:
Output: ________________________
Exercise 6 loop/if-else
Write a Python program that takes the name of a student as input and checks if the student is
present in the class. If the student is present, it should print a message confirming the student's
presence. If the student is not present, it should print a message indicating that the student is
absent.
Teacher Signature: ________________________