AI Lab No.1 Python Basics Final
AI Lab No.1 Python Basics Final
Free and open-source - You can freely use and distribute Python, even for commercial use.
Easy to learn - Python has a very simple and elegant syntax. It's much easier to read and write
Python programs compared to other languages like C++, Java, C#.
Portable - You can move Python programs from one platform to another, and run it without any
changes.
Python is easy to learn. Its syntax is easy and code is very readable.
Python has a lot of applications. It's used for developing web applications, data science, rapid
application development, and so on.
Python allows you to write programs in fewer lines of code than most of the programming
languages.
The popularity of Python is growing rapidly. Now it's one of the most popular programming
languages.
Anaconda Installation
Code:
Output:
Variable: A variable is a named location used to store data in the memory. For example,
Number = 10
Here, we have created a variable named “Number”. We have assigned the value 10 to the
variable.
Code:
Output:
Code:
Output:
Rules and Naming Convention for Variables
Type of variable
The type of variable can be identified by using type ( ) function. It tells which class a variable or
a value belong to.
Code:
Output:
input ( ) : This function first takes the input from the user and then evaluates the expression.
Code:
Output:
Code:
Output:
In Implicit type conversion, Python automatically converts one data type to another data type.
This process doesn‟t need any user involvement.
Code:
Output:
We can see that the new_num has a float data type because Python always converts smaller data
types to larger data types to avoid the loss of data.
Addition of string (higher) data type and integer (lower) data type:
Code:
Output:
2. Explicit Type Conversion:
In Explicit Type Conversion, user converts the data type of an object to required data type. We
use predefined functions like int (), float (), str() etc to perform explicit type conversion.
It is also known as type casting because user casts (changes) the data type of object.
Code:
Output:
After converting num_str to an integer value, Python is able to add these two variables.
Key points
1. Type conversion is the conversion of object from one data type to another data type.
2. Implicit Type Conversion is automatically performed by the Python interpreter.
3. Python avoids the loss of data in Implicit Type Conversion.
4. Explicit Type Conversion is also called Type Casting, the data types of objects are converted
using predefined functions by the user.
5. In Type Casting, loss of data may occur as we enforce the object to a specific data type.
Operators
Operators in general are used to perform operations on values and variables in Python. There
are standard symbols used for the purpose of arithmetic, relational and logical operations. In
this article, we will look into different types of Python operators.
Arithmetic operators:
Arithmetic operators are used to perform mathematical operations like addition, subtraction,
multiplication and division.
Output:
Relational Operators:
Relational operators compare the values. It either returns True or False according to the
condition.
Code:
Output:
Logical operators:
Logical operators perform Logical AND, Logical OR and Logical NOT operations.
Output:
Lab Tasks
11. Write a program to add two numbers by taking input from user.
12. Write a program that takes three different inputs from user and display their type.
13. Write program that takes two numbers as input from user and performs arithmetic
operations.
14. Write program that takes two numbers as input from user and performs relational
operations.
15. Write program that takes two numbers as input from user and performs logical
operations.