0% found this document useful (0 votes)
28 views

CSA Lab 1

This document provides an introduction to basic Python concepts including: - Python is an interpreted, object-oriented programming language that is easy to learn - Key built-in functions like print and input allow outputting text and getting user input - Python has standard data types like numbers, strings, lists, tuples and dictionaries - Operators like arithmetic, comparison, and logical operators are used to perform computations - Control structures like while and for loops are used to iterate through sequences - Examples are provided to demonstrate basic Python programs for lists, dictionaries, user input, and math calculations.

Uploaded by

vol dam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

CSA Lab 1

This document provides an introduction to basic Python concepts including: - Python is an interpreted, object-oriented programming language that is easy to learn - Key built-in functions like print and input allow outputting text and getting user input - Python has standard data types like numbers, strings, lists, tuples and dictionaries - Operators like arithmetic, comparison, and logical operators are used to perform computations - Control structures like while and for loops are used to iterate through sequences - Examples are provided to demonstrate basic Python programs for lists, dictionaries, user input, and math calculations.

Uploaded by

vol dam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Computer System Algorithm (MCS-205) SSUET/QR/114

LAB # 01

BASIC PYTHON
OBJECTIVE:
Getting started with python and its environment.

THEORY:

What is Python?
Python is a modern, easy-to-learn, object-oriented programming language. It has a powerful set of built-in
data types and easy-to-use control constructs. Python is a high level interpreted computer programming
language. High-level languages are programming languages whose instructions closely resemble English. The
alternative are low-level languages, whose instructions are tied to a specific computer architecture and consist
of either numbers or special, non-intuitive four or five letter instructions. Obviously, we prefer to program in
high-level languages. Such languages not only make the programming process faster, they also make
programs easier to debug, and easier to extend and maintain.

Inbuilt Functions:
1. Print:
Python provides the built-in print function to tell the interpreter to print something to the screen. Syntax
for the print function:
Print(expresion)
where expression is any valid Python expression. The interpreter will first evaluate expression to find its
result, then the print function will display that result on the screen

2. Input:
Python provides us with a function that allows us to ask a user to enter some data and returns a reference to
the data in the form of a string. The function is called input.
Python’s input function takes a single parameter that is a string. Syntax for the input function:
Variable = input(“expression”)

Special characters:
Python programs are case sensitive. It would be wrong, for example, to replace print in the program with
Print. Several special characters can be seen (#, ", ()) in the program. They are used in almost every program.
Table 1.1 summarizes their uses.

Character Name Description


() Opening and closing parentheses Used with function
# Pound sign or comment Precedes a comment line
““ Opening and closing quotation marks Encloses a string
(i.e. sequence of characters)
‘‘’ ‘’’ Paragraph comments Encloses a paragraph comment

Data Types:
Data types specify how we enter data into our programs and what type of data we enter. Python Data Types are
used to define the type of a variable. Python has five standard data types – • Numbers (int, float)

Lab 01: Basic Python 1


Name: Tanzeel Ur Rehman Roll no: BMCS22S-002
Computer System Algorithm (MCS-205) SSUET/QR/114

• String
• List
• Tuple
• Dictionary
You can get the data type of any object by using the “type ()” function

Operators:
Operators are special symbols in Python that carry out arithmetic or logical computation Python
divides the operators in the following groups:
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators

Python Arithmetic Operators:


Arithmetic operators are used with numeric values to perform common mathematical operations:

Operator Name Example


+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y

Python Relational Operators:


Comparison operators are used to compare two values:

Operator Name Example


== Equal x==y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

Python Logical Operators:


Logical operators are used to combine conditional statements:

Operator Name Example

Lab 01: Basic Python 2


Name: Tanzeel Ur Rehman Roll no: BMCS22S-002
Computer System Algorithm (MCS-205) SSUET/QR/114

and Return True if both statements are true x < 5 and x < 10
or Return True if one of the statements is true x < 5 or x < 4
not Reverse the result, returns False if the not (x<5 and x< 10)
result is true

Control Structures:
Python provides a standard while statement and a very powerful for statement. The while statement repeats a
body of code as long as a condition is true.
• The while statement:
A while loop executes statements repeatedly as long as a condition remains true.
• The for statement:
A Python for loop iterates through each value in a sequence.

EXERCISE
A. Create a file named lab1.py. Write the following code in the file. Execute it and show the output.
(You can use the Snipping Tool to take a snapshot of your output window).
1. Code:
wordlist = ['cat','dog','rabbit']
letterlist = [ ] for aword in
wordlist: for aletter in aword:
letterlist.append(aletter) print(letterlist)

Output:

2. Code:
capitals = {'Iowa':'DesMoines','Wisconsin':'Madison'}
print(capitals['Iowa']) capitals['Utah']='SaltLakeCity'
print(capitals)
capitals['California']='Sacramento' print(len(capitals))
for k in capitals:

Lab 01: Basic Python 3


Name: Tanzeel Ur Rehman Roll no: BMCS22S-002
Computer System Algorithm (MCS-205) SSUET/QR/114

print(capitals[k]," is the capital of ", k)


Output:

B. Write a Python program which accepts the radius of a circle from the user and compute the area.
Source Code:
# Area of the circle
radius = int(input("Enter thhe Radius of the Circle: ")) pi
= 3.14
Area = (pi * radius * radius) print("Area
of the circle ",Area)
Output:

C. Write a Python program which accepts a sequence of comma-separated numbers from user and
generate a list and a tuple with those numbers.
Source Code:
values = input("Input some comma seprated numbers : ")
list = values.split(",") tuple = tuple(list) print('List : ',list)
print('Tuple : ',tuple)
Output:

Lab 01: Basic Python 4


Name: Tanzeel Ur Rehman Roll no: BMCS22S-002

You might also like