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

Similar documents
Variable and Data Type 2

Downloaded from Chapter 2. Functions

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

CT 229 Java Syntax Continued

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

Introduction to Python, Cplex and Gurobi

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

Introduction to Programming

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

C++ Programming Lecture 11 Functions Part I

Introduction to Engineering gii

Chapter 4: Basic C Operators

Python - Variable Types. John R. Woodward

MYSQL NUMERIC FUNCTIONS

Programming in QBasic

Product Price Formula extension for Magento2. User Guide

Built-in Types of Data

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

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

Standard Library Functions Outline

Structured Programming. Dr. Mohamed Khedr Lecture 4

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

Chapter 3 Mathematical Functions, Strings, and Objects

Green Globs And Graphing Equations

Python Programming: An Introduction to Computer Science

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

Chapter 2. Outline. Simple C++ Programs

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

Methods CSC 121 Fall 2014 Howard Rosenthal

Introduction to MATLAB

Intrinsic Functions Outline

XQ: An XML Query Language Language Reference Manual

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

Fundamentals of Programming & Procedural Programming

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

Process Optimization

Program Structure and Format

4. Modules and Functions

LAB 1 General MATLAB Information 1

Chapter 6 - Notes User-Defined Functions I

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

ANSI C Programming Simple Programs

Subroutines I Computers and Programming

Programming in MATLAB

Maths Functions User Manual

Methods CSC 121 Fall 2016 Howard Rosenthal

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

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

Introduction to Programming


ME 142 Engineering Computation I. Unit 1.2 Excel Functions

PART 1 PROGRAMMING WITH MATHLAB

Mathematica Assignment 1

COLLEGE OF ENGINEERING, NASHIK-4

Matlab as a calculator

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

Dr Richard Greenaway

9 Using Equation Networks

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

C++ Overview. Chapter 1. Chapter 2

Introduction to MATLAB

Using Free Functions

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

Matlab Programming Introduction 1 2

Methods CSC 121 Spring 2017 Howard Rosenthal

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

Introduction to MATLAB 7 for Engineers

Python Programming Exercises 1

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

CS110: PROGRAMMING LANGUAGE I

AMS 27L LAB #1 Winter 2009

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

What is Matlab? The command line Variables Operators Functions

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

Python Intro GIS Week 1. Jake K. Carr

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

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

Script language: Python Data structures

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

Module 01: Introduction to Programming in Python

Math 144 Activity #4 Connecting the unit circle to the graphs of the trig functions

PreCalculus Summer Assignment

Engineering Problem Solving with C++, Etter/Ingber

Basic stuff -- assignments, arithmetic and functions

Summary of basic C++-commands

0.6 Graphing Transcendental Functions

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

Compound Data Types 2

Structured programming

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

C Program Structures

Chapter 1 Introduction to MATLAB

User manual. Version 9.2.0

C Functions. 5.2 Program Modules in C

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

TECH TIP VISION Calibration and Data Acquisition Software

Test #2 October 8, 2015

Transcription:

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

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. For example: 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

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. For example: 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( [ ] ) return its largest item min( [ ] ) return its smallest item sum ( [ ] ) return the sum of a sequence of numbers 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) degrees(x) exp(x) Converts angle x from degrees to radians. Converts angle x from radians to degrees. 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: End

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