(Lab-1 Manual) CS-465 - Python Fundamentals

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

1

CS-465 Fall-2021

GIFT School of Engineering


and Applied Sciences

Fall 2021

CS-465: Artificial Intelligence- Lab

Lab 1 - Manual
Python Installation & Python Language Fundamentals

V1.2

21/12/2020
2
CS-465 Fall-2021

1. Installing on Windows
1. Download the Anaconda installer
2. Double click the installer to launch

Note
If you encounter issues during installation, temporarily disable your anti-virus software during
install, then re-enable it after the installation concludes. If you installed for all users, uninstall
Anaconda and re-install it for your user only and try again.

3. Click Next.
4. Read the licensing terms and click “I Agree”.
5. Select an install for “Just Me” unless you’re installing for all users (which requires
Windows Administrator privileges) and click Next.
6. Select a destination folder to install Anaconda and click the Next button

Note
Install Anaconda to a directory path that does not contain spaces or unicode characters.
Do not install as Administrator unless admin privileges are required.
3
CS-465 Fall-2021

7. Choose whether to add Anaconda to your PATH environment variable. We recommend


not adding Anaconda to the PATH environment variable, since this can interfere with other
software. Instead, use Anaconda software by opening Anaconda Navigator or the
Anaconda Prompt from the Start Menu.

8. Choose whether to register Anaconda as your default Python. Unless you plan on
installing and running multiple versions of Anaconda or multiple versions of Python,
accept the default and leave this box checked.
9. Click the Install button. If you want to watch the packages Anaconda is installing, click Show
Details.
10. Click the Next button.
11. Optional: To install PyCharm for Anaconda, click on the link
to https://www.anaconda.com/pycharm. Or to install Anaconda without PyCharm, click the
Next button.
4
CS-465 Fall-2021

12. After a successful installation you will see the “Thanks for installing Anaconda” dialog box:

Verifying your installation


You can confirm that Anaconda is installed and working with Anaconda Navigator or conda.

Anaconda Navigator
Anaconda Navigator is a graphical user interface that is automatically installed with Anaconda.
Navigator will open if the installation was successful.
 Windows: Click Start, search or select Anaconda
Navigator from the menu.
5
CS-465 Fall-2021

2. Control Structures
Conditional Statements: if, else, elif
Often, we want to execute the code to perform some action based on a condition. For example, if
the condition is true, do something. We can get this done using “if, else, elif” statements.
Python’s “if” statement select actions to perform.
Let’s learn with a simple example.

True
Notice a block of whitespace before the print( ) statement, in the cell above. It is very
important in Python. Python does not use brackets in order to separate a block of code in the
execution statements, it uses white spaces instead. Like most of the other IDEs, jupyter notebook
automatically does this indentation of Python code after a colon.
If the condition is not satisfied (in the example below), print statement will not be
executed. Nothing will appear in the output.

So, in the above cell, the condition is not satisfied and we did not get True in the output. Well, do
we really know if the code was executed!
In such situation, we usually want our program to do something, even if the condition is not
satisfied, e.g., print False. This is where we (can) introduce else.

False
We can have multiple conditions in the code! Let’s introduce elif in such situation. * (elif is a short of
else-if and we can introduce many elif statements in the code.) *
6
CS-465 Fall-2021

Statement-02
** Very important thing to remember for elif statement. ** The code in the “if-elif(s)-else” block
will only execute the first True condition, it does not matter if you have other True conditions in
the code after your first True.
Let’s try another example here!

Statement-02
In the code above, although 2==2 is True, however, print(‘3rd statement’) will not be executed.
Since, the condition for print(‘2nd statement’) is also True and have already been executed!

3. Catching Exceptions using try and except


The idea of try and except is that you know that some sequence of instruction(s) may have a
problem and you want to add some statements to be executed if an error occurs.
These extra statements (the except block) are ignored if there is no error.
Let’s try to write a code to take an input as float.

After the user enters incorrect input. An exception will be thrown as shown below:

Let’s see how we can use try and except to handle this exception.

You might also like