Python Language Programming Lab
Laboratory Notes (1)
Topic:
1. To write a python program that takes in command line arguments as input
and print the number of arguments
Objective:
To write a python program that takes in command line arguments as input and print the
number of arguments.
Description:
Steps to save and run python programs:
1. Open a text editor
2. Type the instructions
3. Save the python script in a file with filename having extension .py
4. To execute the python script, Run the interpreter with the command prompt
python progam_name.py
Print statement: An instruction that causes the Python interpreter to display a value on
the screen.
Ex: Type the following instruction
python
>>> print 4
Displays 4 as output
Input Statement: An instruction used to get input from the user.
Ex: # Get numbers from user
n1 = input('Please enter first number= ')
n2= input('Please enter second number= ')
n3= input('Please enter third number= ')
# sum of three numbers
, total= float(n1) + float(n2)+ float(n3)
print('The sum of three numbers =', total)
Data Types in Python numeric, string, Boolean, list, tuple and dictionary
Numeric: Integers, floating point numbers and complex numbers falls under Python
numbers category. They are defined as int, float and complex class in Python.
Integers can be of any length, it is only limited by the memory available. A floating point
number is accurate up to 15 decimal places. Integer and floating points are separated by
decimal points. 1 is integer, 1.0 is floating point number. Complex numbers are written in
the form, x + yj, where x is the real part and y is the imaginary part.
String is sequence of Unicode characters. We can use single quotes or double quotes to
represent strings. Multi-line strings can be denoted using triple quotes, ''' or """.
Ex: s = 'Hello world!'
# s[4] = 'o'
print("s[4] = ", s[4])
Boolean: Returns True or False
Ex: print(a==b)
Output: True
List: It is an ordered sequence of items. It is one of the most used datatype in Python and is
very flexible. All the items in a list do not need to be of the same type. We can use the slicing
operator [ ] to extract an item or a range of items from a list. Index starts form 0 in Python.
Ex: a=[1,2.2,’python’]
a=[1,2,3,4]
a[2]=10
a
Output:[1,2,10,4]
Tuple: is an ordered sequence of items same as list. The only difference is that tuples are
immutable. Tuples once created cannot be modified. Tuples are used to write-protect data
and are usually faster than list as it cannot change dynamically. It is defined within
parentheses () where items are separated by commas.
Ex: t = (5,'program', 1+3j)
Laboratory Notes (1)
Topic:
1. To write a python program that takes in command line arguments as input
and print the number of arguments
Objective:
To write a python program that takes in command line arguments as input and print the
number of arguments.
Description:
Steps to save and run python programs:
1. Open a text editor
2. Type the instructions
3. Save the python script in a file with filename having extension .py
4. To execute the python script, Run the interpreter with the command prompt
python progam_name.py
Print statement: An instruction that causes the Python interpreter to display a value on
the screen.
Ex: Type the following instruction
python
>>> print 4
Displays 4 as output
Input Statement: An instruction used to get input from the user.
Ex: # Get numbers from user
n1 = input('Please enter first number= ')
n2= input('Please enter second number= ')
n3= input('Please enter third number= ')
# sum of three numbers
, total= float(n1) + float(n2)+ float(n3)
print('The sum of three numbers =', total)
Data Types in Python numeric, string, Boolean, list, tuple and dictionary
Numeric: Integers, floating point numbers and complex numbers falls under Python
numbers category. They are defined as int, float and complex class in Python.
Integers can be of any length, it is only limited by the memory available. A floating point
number is accurate up to 15 decimal places. Integer and floating points are separated by
decimal points. 1 is integer, 1.0 is floating point number. Complex numbers are written in
the form, x + yj, where x is the real part and y is the imaginary part.
String is sequence of Unicode characters. We can use single quotes or double quotes to
represent strings. Multi-line strings can be denoted using triple quotes, ''' or """.
Ex: s = 'Hello world!'
# s[4] = 'o'
print("s[4] = ", s[4])
Boolean: Returns True or False
Ex: print(a==b)
Output: True
List: It is an ordered sequence of items. It is one of the most used datatype in Python and is
very flexible. All the items in a list do not need to be of the same type. We can use the slicing
operator [ ] to extract an item or a range of items from a list. Index starts form 0 in Python.
Ex: a=[1,2.2,’python’]
a=[1,2,3,4]
a[2]=10
a
Output:[1,2,10,4]
Tuple: is an ordered sequence of items same as list. The only difference is that tuples are
immutable. Tuples once created cannot be modified. Tuples are used to write-protect data
and are usually faster than list as it cannot change dynamically. It is defined within
parentheses () where items are separated by commas.
Ex: t = (5,'program', 1+3j)