You are on page 1of 7

Lab 01: Introduction to Python

Objectives

• To acquaint the students with IDLE for Python programming.


• To understand the basic structure of python program

Installation link of Python IDLE

Latest python version can be downloaded from


https://www.python.org/downloads/windows/

Different IDEs for Python

• IDLE
• PyCharm
• Spyder
• Thonny
• Atom
• PyDev
• Visual Studio Code
• Sublime Text
• Wing IDE
• Visual Studio
• Pyscripter
• Jupyter

Basic Structure

Python is a high-level, interpreted programming language that is easy to learn and use. It has a simple and
easy-to-understand syntax that emphasizes readability and reduces the cost of program maintenance. There
is no use of curly braces or semicolon in Python programming language. It is English-like language. But
Python uses the indentation to define a block of code. Indentation is nothing but adding whitespace before
the statement when it is needed. The basic structure of a Python program consists of the following
components:

1. Comments: Comments are used to explain the purpose of the code or to make notes for other

programmers. They start with a ‘#’ symbol and are ignored by the interpreter.
2. Import Statements: Import statements are used to import modules or libraries into the program.

These modules contain predefined functions that can be used to accomplish tasks.

3. Variables: Variables are used to store data in memory for later use. In Python, variables do not

need to be declared with a specific type.

4. Data Types: Python supports several built-in data types including integers, floats, strings,

Booleans, and lists.

5. Operators: Operators are used to perform operations on variables and data. Python supports

arithmetic, comparison, and logical operators.

6. Control Structures: Control structures are used to control the flow of a program. Python supports

if-else statements, for loops, and while loops.

7. Functions: Functions are used to group a set of related statements together and give them a name.

They can be reused throughout a program.

8. Classes: Classes are used to define objects that have specific attributes and methods. They are used

to create more complex data structures and encapsulate code.

9. Exceptions: Exceptions are used to handle errors that may occur during the execution of a program.

Overall, the basic structure of a Python program consists of these components working together to
accomplish a specific task or solve a particular problem.

Initializing Python IDLE

Procedure
It lets the user write source code of Python program by following these steps.

1. Click on File on the shell and from the drop-down list select New File.
2. The following window will appear, choose file -> new to create a new .py file.
3. Write the source code as follows and save the file.

4. Save the program with the extension .py.

5. Program must be saved every time before execution.


6. Short hand for running a program is fn+f5 or Run option can be chosen
7. If the program runs successfully, output will be shown in the shell otherwise an error
window will be displayed.
Example Code

# This is a comment

import random # Importing a module.

# Defining variables
x = 10
y = "Hello, World!"
z = True

# Performing arithmetic operation


result = x + 5

# Using if-else statement


if z:
print(y)
else:
print(result)

# Defining a function
def greet(name):
print("Hello, " + name + "!")

# Using the function


greet("Alice")

Data types

Python has the following data types of built-in by default, in these categories:

Text Type: str

Numeric Types: int, float, complex


Sequence Types: list, tuple, range

Mapping Type: dict

Set Types: set, frozenset

Boolean Type: bool

Binary Types: bytes, bytearray, memoryview

None Type: NoneType

Printing the data type of a variable:

x=10
Print(type(x))

Practice

Find the type of following variables.

x=20, y=” NUST”, j=False, z= 3.1445

convert type of x to float, z to integer using type conversion.

Try it

Import random

Print(random.randrange(1,10))

Print(“/n”*100)

Setting a Specific datatype


You can also set a specific datatype for a variable.

x = str(3) # x will be '3'


y = int(3) # y will be 3
z = float(3) # z will be 3.0
w=complex(1j) #w will 1j
In Lab Tasks

1. Write a python code for the following.

a. displays the statement “Hello world!”


b. “Hello” and “world” should be displayed on different lines using multiple print statement.
c. “Hello” and “world” should be displayed on different lines using a single print statement.

2. DATA TYPES
a. Assign a floating value to a variable and then check its type.
b. Modify the above program by converting the floating type into integer and checks its type
again.
c. Prompt the user to enter a number and save it in an integer variable.

3. What does the following expression evaluate to?


a. 6 + 5 * 4 % 3=
b. 12 / 3 * 3 =
c. 10 % 3 – 6 / 2 =
d. 5* * 2 / (4 * 2) =

Complete Programs

1. Write a program which inputs a temperature reading expressed in Fahrenheit and


outputs its equivalent in Celsius, using the formula:
 C = 5/9(F- 32)
Compile and run the program. Its behavior should resemble this:
Temperature in Fahrenheit: 41
41 degrees Fahrenheit = 5 degrees Celsius

2. Assume that there are 7.481 gallons in a cubic foot. Write a program that asks the user to enter several
gallons and then displays the equivalent in the cubic foot.

3. Write a program that asks for your height in integer inches and then convert your height into feet and inches

POST LAB

1.Write a program that requests the user to enter the current world population and the current
population of the U.S. (or of some other nation of your choice). Store the information in
variables of type int. Have the program display the percent that the U.S. (or other nation’s)
population is of the world’s population. The output should look something like this:
Enter the world's population: 6888
Enter the population of the US: 310
The population of the US is 4.5% of the world population.

2. Write a program that asks how many miles you have driven and how many gallons of gasoline
you have used and then reports the miles per gallon your car has gotten. The program can request
distance in kilometers and petrol in liters and then report the result in liters per 100 kilometers.
3. A palindrome is a word, phrase, or sequence of numbers that read the same backward as forward.
Write a program which detects if input sequence is palindrome or not.

You might also like