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

Lab Task # 2 Introduction To Programming With Python

The document describes a lab assignment for an Artificial Intelligence course. It introduces students to basic Python programming concepts like variables, arithmetic operations, conditionals, loops, functions and input/output. It provides examples and 12 tasks for students to write simple Python programs implementing these basic concepts, like a calculator, computing expressions, finding average, tables, even/odd numbers etc. The overall aim is to familiarize students with the Python programming environment and basic commands.

Uploaded by

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

Lab Task # 2 Introduction To Programming With Python

The document describes a lab assignment for an Artificial Intelligence course. It introduces students to basic Python programming concepts like variables, arithmetic operations, conditionals, loops, functions and input/output. It provides examples and 12 tasks for students to write simple Python programs implementing these basic concepts, like a calculator, computing expressions, finding average, tables, even/odd numbers etc. The overall aim is to familiarize students with the Python programming environment and basic commands.

Uploaded by

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

ISRA UNIVERSITY

Department of Computer Science

(CSAI-412) Artificial Intelligence


Lab Task # 02
Fall 2023
Instructor: Areeba Naeem
Date: 10-JAN-2023
WEEK # 2

Introduction to Programming
with Python

The aim of this lab is to familiar students with environment and basic command
of Python.

Objectives: After completing this lab the students will be able


 To explain the Python programming environment
 To print any message on the screen and get input from user by using Print and Input commands.
 To create variable and store values in variables and perform basic arithmetic operations on them.
 To repeat a statement by using for loop
 To import a library in 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.

Command name Description

abs(value) absolute value

ceil(value) rounds up

cos(value) cosine, in radians

floor(value) rounds down

log(value) logarithm, base e

log10(value) logarithm, base 10

max(value1, value2) larger of two values

min(value1, value2) smaller of two values

round(value) nearest whole number


sin(value) sine, in radians

sqrt(value) square root

 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.

The for loop


 For loop: Repeats a set of statements over a group of values.
 Syntax:
For variableName in groupOfValues:
statements
 We indent the statements to be repeated with tabs or spaces.
 variableName gives a name to each value, so you can refer to it in the
statements.
 groupOfValues can be a range of integers, specified with the range
function.
 Example:
for x in range(1, 6):
print x

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.

You might also like