Lab Task # 2 Introduction To Programming With Python
Lab Task # 2 Introduction To Programming With Python
Introduction to Programming
with Python
The aim of this lab is to familiar students with environment and basic command
of Python.
Programming basics
Code or Source Code: The sequence of instructions in a program.
Syntax: The set of legal structures and commands that can be used in a particular
programming language.
Output: The messages printed to the user by a program.
Console: The text box onto which output is printed.
Some source code editor’s pop up the console as an external window, and others
contain their own console window.
Compiling and interpreting
Many languages require you to compile (translate) your program into a form that the
machine understands.
Python is instead directly interpreted into machine instructions.
Print
Print: Produces text output on the console.
Syntax:
Print "Message"
Print Expression
Prints the given text message or expression value on the console, and moves the cursor
down to the next line.
Print Item1, Item2, ..., ItemN
Prints several messages and/or expressions on the same line.
Examples:
Print "Hello, world!"
age = 45
Print "You have", 65 - age, "years until retirement"
Output:
Hello, world!
You have 20 years until retirement
Expressions
Expression: A data value or set of operations to compute a value.
Examples: 1+4*3
42
Arithmetic operators we will use:
+-*/ addition, subtraction/negation, multiplication, division
% modulus, a.k.a. remainder
** Exponentiation
Precedence: Order in which operations are computed.
* / % ** have a higher precedence than + -
1 + 3 * 4 is 13
Parentheses can be used to force a certain order of evaluation.
(1 + 3) * 4 is 16
Task # 01: Write a program in Python language of basic calculator that can perform basic
arithmetic operations.
Math commands
Python has useful commands for performing calculations.
ceil(value) rounds up
To use many of these commands, you must write the following at the top of your
Python program:
from math import *
Variables
Variable: A named piece of memory that can store a value.
Usage:
Compute an expression's result,
Store that result into a variable,
And use that variable later in the program.
Assignment statement: Stores a value into a variable.
Syntax:
Name = value
Examples: x=5
gpa = 3.14
X 5 gpa 3.14
A variable that has been given a value can be used in expressions. x + 4 is 9
Task # 02:
Write a program in Python programming of a2+2ab+b2.
Task # 03:
Write a program in Python of showing students marks of five subjects and their percentage.
Task# 04:
Write a program in Python to show Average of five values.
Input
input : Reads a number from user input.
You can assign (store) the result of input into a variable.
Example:
age = input("How old are you? ")
print "Your age is", age
print "You have", 65 - age, "years until retirement"
Output:
How old are you? 53
Your age is 53
You have 12 years until retirement
Task# 05: Write a Python program that prompts the user for his subject’s marks and finally report
his subject marks in PGR format with percentage.
Task# 06:
Write a Python program that can print a Table of 5 by using For Loop.
Task# 07:
Write a Python program that can take a number from user and print its Table.
Task# 08:
Write a Python program that can take three different numbers from user and print their tables on
screen.
Task# 09:
Write a Python program that can take two values from user and then show their Addition,
Subtraction, Division and Multiplication on screen.
Task# 10:
Write a Python program that can show Square of numbers from 1 to 100.
Task# 11:
Write a Python program that can show Even numbers from 1 to 100.
Task# 12:
Write a Python program that can show Odd number up to 100.