Variable and Data Type 2

Similar documents
Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

Downloaded from Chapter 2. Functions

CT 229 Java Syntax Continued

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

Introduction to Programming

Introduction to Python, Cplex and Gurobi

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

Selection Statement ( if )

C++ Programming Lecture 11 Functions Part I

Chapter 4: Basic C Operators

Variable and Data Type I

Introduction to Engineering gii

Product Price Formula extension for Magento2. User Guide

Python - Variable Types. John R. Woodward

MYSQL NUMERIC FUNCTIONS

Arithmetic. 2.2.l Basic Arithmetic Operations. 2.2 Arithmetic 37

Programming in QBasic

Built-in Types of Data

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

Standard Library Functions Outline

Variable and Data Type I

The Number object. to set specific number types (like integer, short, In JavaScript all numbers are 64bit floating point

XQ: An XML Query Language Language Reference Manual

Methods CSC 121 Fall 2014 Howard Rosenthal

C++ Overview. Chapter 1. Chapter 2

Structured Programming. Dr. Mohamed Khedr Lecture 4

12. Numbers. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

COLLEGE OF ENGINEERING, NASHIK-4

Chapter 3 Mathematical Functions, Strings, and Objects

Green Globs And Graphing Equations

Python Programming: An Introduction to Computer Science

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

PART 1 PROGRAMMING WITH MATHLAB

Methods CSC 121 Fall 2016 Howard Rosenthal

Maths Functions User Manual

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

Chapter 2. Outline. Simple C++ Programs

Fundamentals of Programming & Procedural Programming

Program Structure and Format

Introduction to MATLAB

Intrinsic Functions Outline

Methods CSC 121 Spring 2017 Howard Rosenthal

Python Intro GIS Week 1. Jake K. Carr

C Program Structures

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

4. Modules and Functions

LAB 1 General MATLAB Information 1

Introduction to MATLAB. Computational Probability and Statistics CIS 2033 Section 003

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

Dr Richard Greenaway

Mathematica Assignment 1

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

Programming in MATLAB

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Process Optimization

Script language: Python Data structures

ME 142 Engineering Computation I. Unit 1.2 Excel Functions

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Chapter 6 - Notes User-Defined Functions I

Introduction to MATLAB

ANSI C Programming Simple Programs

Subroutines I Computers and Programming

Python Programming Exercises 1

Introduction to Programming


Module 01: Introduction to Programming in Python

3.1. Chapter 3: The cin Object. Expressions and Interactivity

Matlab as a calculator

9 Using Equation Networks

How To Think Like A Computer Scientist, chapter 3; chapter 6, sections

Introduction to Python

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY

AMS 27L LAB #1 Winter 2009

Mat 2170 Week 6. Methods. Week 6. Mat 2170 Week 6. Jargon. Info Hiding. Math Lib. Lab 6. Exercises. Methods. Spring 2014

Using Free Functions

Operators Functions Order of Operations Mixed Mode Arithmetic VOID Data. Syntax and type conventions Using the Script window interface

Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions. Grace Murray Hopper. Arithmetic Expressions.

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

2 Making Decisions. Store the value 3 in memory location y

Matlab Programming Introduction 1 2

ECET 264 C Programming Language with Applications

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Chapter 8 Dictionaries. Dictionaries are mutable, unordered collections with elements in the form of a key:value pairs that associate keys to values.

Finding Pi: Applications of Loops, Random Numbers, Booleans CS 8: Introduction to Computer Science, Winter 2018 Lecture #6

Computer Programming : C++

Lecture 1. Introduction to course, Welcome to Engineering, What is Programming and Why is this the first thing being covered in Engineering?

Introduction to MATLAB 7 for Engineers

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

Julia Calculator ( Introduction)

Introduction to Matlab

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Sketchify Tutorial Properties and Variables. sketchify.sf.net Željko Obrenović

CS110: PROGRAMMING LANGUAGE I

Computer Sciences 368 Scripting for CHTC Day 3: Collections Suggested reading: Learning Python

Transcription:

The Islamic University of Gaza Faculty of Engineering Dept. of Computer Engineering Intro. To Computers (LNGG 1003) Lab 3 Variable and Data Type 2 Eng. Ibraheem Lubbad March 2, 2017

Python Lists: Lists are Python's compound data types. A list contains items separated by commas and enclosed within square brackets ([]). All the items belonging to a list can be of different data type. The values stored in a list can be accessed using the slice operator ([ ] and [:]) with indexes starting at 0 in the beginning of the list and working their way to end -1. Example 1: =["apple", "orange",100,25.5] [0] 'apple' >>> 3*items[:2] ['apple', 'orange', 'apple', 'orange', 'apple', 'orange'] [2]=items[2]+50 ['apple', 'orange', 150, 25.5] [0:2]=[20,30] # replace some elements [20, 30, 150, 25.5] [0:2]=[] # remove some elements [150, 25.5] [2:2]=[200,400] # insert some elements [150, 25.5, 200, 400] >>> del items[0] # delete element at index 0 from list [25.5, 200, 400].append("orange") # List.append(object) -- append object to end [25.5, 200, 400, 'orange'] >>> 200 in items # check existing item in list True >>> 200 not in items # check not existing item in list False

List Methods: name Description count ( value ) Returns the number of times a given value appears in the list Index( value) Returns the lowest index of a given element (value ) within the list extend(seq ) This method does not return any value but add the content sequence to existing list. insert(index,object) Inserts a new element (object) before the element at a given index. Increases the length of the list by one. append ( object ) Adds a new element (object) to the end of the list. remove(value) Removes the first occurrence (lowest index) of a given element (value) from the list. Produces an error if the element is not found. reverse() reverses the elements in the list. The list is modified sort Sorts the elements of the list in ascending order. The list is modified Python Tuples: A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas, tuples are enclosed within parentheses (). Tuples its read only, that s means are immutable. The main differences between lists and tuples are: Lists are enclosed in brackets ([ ]) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated.

Examples: Example 2: >>> t=("orange",55,3.5) >>> t[0] 'orange' >>> t[1:3] (55, 3.5) >>> t[-1] 3.5 >>> len(t) 3 >>> # Tuples may be nested >>> u=(t,(1,2,3)) >>> u (('orange', 55, 3.5), (1, 2, 3)) >>> len(u) 2 >>> # Tuples are immutable: >>> t[0]=100 # not allow to update tuple Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: 'tuple' object does not support item assignment Tuple Methods: name count ( value ) Index( value) Description Returns the number of times a given value appears in the tuple Returns the lowest index of a given element (value ) within the tuple

Python Dictionary: Python's dictionaries are kind of hash table type consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. A dictionary Values can be any arbitrary Python object. Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([]). Dictionaries have the elements are "out of order", they are simply unordered. Examples: Example 3: >>> dict={'name':'ali', 'code':12016000,'dept':"engineering"} >>> dict['name'] 'ali' >>> dict.keys() ['dept', 'code', 'name'] >>> dict.values() ['Engineering', 12016000, 'ali'] >>> dict2={ } >>> dict2[ "one" ]=" this is one" #add a new element to dictionary >>> dict2 [2]= "this is two" >>> dict {2: 'this is two', 'one': ' this is one'} >>> del dict["one"] # delete element has a key one from dictionary >>> dict {2: 'this is two'} >>> dict.clear() # delete all elements >>> dict {}

Mathematical functions: 1. Build-in Functions The Python interpreter has a number of functions built into it that are always available: abs(x) The absolute value of x. cmp(x, y) -1 if x < y, 0 if x == y, or 1 if x > y max( seq ) return its largest item in sequence (String,List, Tuple ) min( seq) return its smallest item n sequence (String,List, Tuple ) sum (seq ) return the sum of a sequence (List, Tuple) of numbers len(seq) Return number of elements in sequence (String,List, Tuple ) Example 4: print "abs(-5) = ",abs(-5) print "list=[88,5,9], largest number in list is = ",max([88,5,9]) print "list=[88,5,9], smallest number in list is = ",min([88,5,9]) print "sum of a sequence of numbers [8,6,2,5] = ",sum([8,6,2,5]) print "compare two number :",cmp(8,5) Output: 2. Math module functions : The following functions are provided by this module, before use it we must invoke module using import keyword: ceil(x) Return the ceiling of x as a float, the smallest integer value greater than or equal to x. floor(x) Return the floor of x as a float, the largest integer value less than or equal to x. fabs(x) Return the absolute value of x as a float. sqrt(x) The square root of x for x > 0 log(x) The natural logarithm of x, for x> 0

log10(x) The base-10 logarithm of x for x> 0 Trigonometric Functions Return value of function in radians sin(x), cos(x), tan(x) radians(x) Converts angle x from degrees to radians. degrees(x) Converts angle x from radians to degrees. exp(x) The exponential of x: ex Constants: pi The mathematical constant π = 3.141592... e The mathematical constant e = 2.718281... How to use it: Example 5: import math # firstly we must import math module print "sin(90) = ",math.sin(math.pi/2) print "cos(180) = ",math.cos(math.pi) print "fabs(-5)=", math.fabs(-5) print "floor(5.9)=",math.floor(5.9) print "ceil(5.9)=",math.ceil(5.9) print "sqrt(49) =",math.sqrt(49) print "log10(1000) =",math.log10(1000) print "pi = ", math.pi print "degrees(math.pi/2) =",math.degrees(math.pi/2) print "e^2 = ",math.exp(2) print "log e ^2 = ",math.log(math.exp(2)) Output:

Exercises: 1) Write a Python program that compute the area of a circle given the radius entered by the user. Area = πr 2 2) Write a Python program to solve quadratic equations of the form ax 2 + bx + c = 0 Where the coefficients a, b, and c are real numbers taken from user. The two real number solutions are derived by the formula x = b ± b2 4ac 2a For this exercise, you may assume that a 0 and b 2 4ac 3) Write a program that define dictionary will save the following information entered by the user and then use it to print a payroll statement as the following example: Employee s name (e.g., ibraheem) Number of hours worked in a week (e.g., 13) Hourly pay rate (e.g., 6.75) Municipality tax rate (e.g., 20%) Country tax rate (e.g., 8%) Hint: dictionary will save information just about (Employee s name, Number of hours worked in a week, Hourly pay rate and Deductions). Output as the following image: