CSA Lab 1
CSA Lab 1
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.
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)
• 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
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:
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: