You are on page 1of 15

Lab No.

About Python Programming

 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.

Why Learn Python?

 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

1. Download Anaconda Installer.


2. Run it as administrator. The installation process will be started as follows:

3. Click on Next button.


4. Click on I Agree button.
5. You can choose any option either just me or All users. Then Click Next button.

6. Click on Next button.


7. Click on Install button. The installation will be started as follows:
8. Click on Next button.
9. Click on finish button. The anaconda navigator is installed now.
10. Open anaconda navigator and launch Spyder.

First Python Program

Write a program in python that prints „Hello World‟.

Code:

Output:

Creating Variables and Assigning values

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:

Assigning multiple variables to multiple variables:

Code:

Output:
Rules and Naming Convention for Variables

1. Variable names should have a combination of letters in lowercase (a to z) or uppercase ( A to


Z) or digits (0 to 9) or an underscore (_). For example:
snake_case
camelCase
CapWords
2. Create a name that makes sense. For example, vowel makes more sense than v.
3. If you want to create a variable name having two words, use underscore to separate them. For
example,
my_name
current_salary
4. Never use special symbols like !, @, #, $, % etc.
5. Do not start a variable name with a digit.

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:

Taking input in python

input ( ) : This function first takes the input from the user and then evaluates the expression.
Code:

Output:

How the input function works in Python:


 When input ( ) function executes program flow will be stopped until the user has given an
input.
 Whatever you enter as input, input function convert it into a string. If you enter an integer
value still input ( ) function convert it into a string. You need to explicitly convert it into an
integer in your code using typecasting.

Code:

Output:

Python Type Conversion (Casting)


The process of converting the value of one data type (integer, string, float etc) to another data
type is called type conversion. Python has two types conversion.

1. Implicit type conversion


2. Explicit type conversion
1. Implicit type conversion

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.

We got the num_sum value and data type to be an integer.

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.

Operator Description Syntax


+ Addition: adds two operands x+y
- Subtraction: subtracts two operands x-y
* Multiplication: multiplies two operands x*y
/ Division (float): divides the first operand by the second x/y
// Division (float): divides the first operand by the second x // y
% Modulus: returns the remainder when first operand is divided by the second x%y
** Power : Returns first raised to power second x ** y
Code:

Output:
Relational Operators:

Relational operators compare the values. It either returns True or False according to the
condition.

Operator Description Syntax


> Greater than: True if left operand is greater than the right x>y
< Less than: True if left operand is less than the right x<y
== Equal to: True if both operands are equal x == y
!= Not equal to - True if operands are not equal x != y
>= Greater than or equal to: True if left operand is greater than or equal to the right x >= y
<= Less than or equal to: True if left operand is less than or equal to the right x <= y

Code:

Output:
Logical operators:

Logical operators perform Logical AND, Logical OR and Logical NOT operations.

Operator Description Syntax


and Logical AND: True if both the operands are true x and y
or Logical OR: True if either of the operands is true x or y
not Logical NOT: True if operand is false not x
Code:

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.

You might also like