Variable and Data Type I

Similar documents
Variable and Data Type I

Selection Statement ( if )

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

ENGR 101 Engineering Design Workshop

PROGRAMMING FUNDAMENTALS

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

Chapter 3 : Informatics Practices. Class XI ( As per CBSE Board) Python Fundamentals. Visit : python.mykvs.in for regular updates

CMSC 201 Computer Science I for Majors

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

CSCE 110 Programming I

Getting Started with Python

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Fundamentals of Programming (Python) Getting Started with Programming

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

Variables, expressions and statements

Python Input, output and variables

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

Python Input, output and variables. Lecture 22 COMPSCI111/111G SS 2016

Chapter 2 Writing Simple Programs

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

Python - Variable Types. John R. Woodward

UNIVERSITÀ DI PADOVA. < 2014 March >

Full file at

Constants. Variables, Expressions, and Statements. Variables. x = 12.2 y = 14 x = 100. Chapter

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

BASIC ELEMENTS OF A COMPUTER PROGRAM

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Basic Syntax - First Program 1

Python memento TI-Smart Grids

Introduction to Computers. Laboratory Manual. Experiment #3. Elementary Programming, II

C++ Programming: From Problem Analysis to Program Design, Third Edition

Variables, Expressions, and Statements

Programming with Python

About Variables in Python F E B 1 1 T H

DaMPL. Language Reference Manual. Henrique Grando

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

The float type and more on variables FEB 6 TH 2012

Lab1. Introduction to Python. Lab 4: Selection Statement. Eng. Mai Z. Alyazji

Jython. secondary. memory

Introduction to Python, Cplex and Gurobi

Algorithms and Data Structures

PYTHON- AN INNOVATION

SI Networked Computing: Storage, Communication, and Processing, Winter 2009

And Parallelism. Parallelism in Prolog. OR Parallelism

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

D R S H YA M N C H AW D A

Here n is a variable name. The value of that variable is 176.

Introduction to Python

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

Variables and Constants

The Three Rules. Program. What is a Computer Program? 5/30/2018. Interpreted. Your First Program QuickStart 1. Chapter 1

Scripting Languages. Python basics

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

Introduction to Python

CSC Web Programming. Introduction to JavaScript

Eng. Mohammed S. Abdualal

Key Differences Between Python and Java

Hello, World and Variables

Review. Input, Processing and Output. Review. Review. Designing a Program. Typical Software Development cycle. Bonita Sharif

Python for ArcGIS. Lab 1.

CS 115 Exam 1, Fall 2015 Thu. 09/24/2015

Computer System and programming in C

These are reserved words of the C language. For example int, float, if, else, for, while etc.

PYTHON MOCK TEST PYTHON MOCK TEST III

DEPARTMENT OF MATHS, MJ COLLEGE

egrapher Language Reference Manual

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Chapter 4 : Informatics Practices. Data Handling. Class XI ( As per CBSE Board) Visit : python.mykvs.in for regular updates

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Computer Programming : C++

FOURTH GRADE Mathematics Standards for the Archdiocese of Detroit

>>> * *(25**0.16) *10*(25**0.16)

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

CEN 414 Java Programming

Sketchpad Graphics Language Reference Manual. Zhongyu Wang, zw2259 Yichen Liu, yl2904 Yan Peng, yp2321

MICROPROCESSOR SYSTEMS INTRODUCTION TO PYTHON

ENGR 102 Engineering Lab I - Computation

Variable and Data Type 2

Lexical Considerations

Built-in Types of Data

Data Type Fall 2014 Jinkyu Jeong

3. Java - Language Constructs I

Ex: If you use a program to record sales, you will want to remember data:

Full file at C How to Program, 6/e Multiple Choice Test Bank

Datatypes, Variables, and Operations

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

Introduction to Computer Programming for Non-Majors

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Table of Contents EVALUATION COPY

Unit 3. Operators. School of Science and Technology INTRODUCTION

CSI33 Data Structures

Chapter 17. Fundamental Concepts Expressed in JavaScript

Beyond Blocks: Python Session #1

cs1114 REVIEW of details test closed laptop period

Work relative to other classes

Python. Objects. Geog 271 Geographic Data Analysis Fall 2010

Course Outline Introduction to C-Programming

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Transcription:

Islamic University Of Gaza Faculty of Engineering Computer Engineering Department Lab 2 Variable and Data Type I Eng. Ibraheem Lubbad September 24, 2016

Variable is reserved a location in memory to store values, based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables. Assigning Values to Variables Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. Multiple Assignment: Python allows you to assign a single value to several variables simultaneously. For example: Example 1: a = b = c = 1 Here, an integer object is created with the value 1, and all three variables are assigned to the same memory location. You can also assign multiple values to multiple variables. For example: Example 2: a, b, c = 1, 2, "IUG" Identifiers are the names that identify the elements such as classes, methods, and variables in a program, all identifiers must obey the following rules: Identifiers must contain at least one character. The first character must be an alphabetic letter (upper or lower case) or the underscore (_) The remaining characters (if any) may be alphabetic characters (upper or lower case), the underscore, or a digit No other characters (including spaces) are permitted in identifiers. A reserved word (Python Keywords) cannot be used as an identifier. and del from None try as elif global nonlocal True assert else if not while break except import or with class False in pass yield continue finally is raise def for lambda return Python keywords

Standard Data Types: Python has various standard data types that are used to define the operations possible on them and the storage method for each of them. Python has 5 standard data types: Numbers String List Tuple Dictionary *In this lab we will take just about Numbers and String Python Numbers: Number data types store numeric values. Number objects are created when you assign a value to them. For example: Example 3 var1 = 10 var2 = 1.5 var3 = 2.5-1j Python supports four different numerical types: int (signed integers) long (long integers) float (floating point real values) complex (complex numbers) int long float complex 10 51924361L 15.20 3.14j -786 0xDEFABCECBDAECBFBAEl 32.3+e18 4.53e-7j Python displays long integers with a lowercase l or an uppercase L. A complex number consists of an ordered pair of real floating-point numbers denoted by x + yj, where x is the real part and b is the imaginary part of the complex number.

Python Arithmetic Operators: Assume variable a holds 2 and variable b holds 5, then: Operator Example + Addition a + b = 7 - Subtraction a b = -3 * Multiplication a * b = 10 / Division b / a = 2 % Modulus b % a = 1 ** Exponent a**b =32 // Floor Division 9//2 = 4 and 9.0//2.0 = 4.0 but 9/2 = 4 and 9.0/2.0 = 4.5 Python Assignment Operators: Operator Example = Assign c = a + b assigns value of a + b into c += Add AND Assign c += a is equivalent to c = c + a -= Subtract AND Assign c -= a is equivalent to c = c - a *= Multiply AND Assign c *= a is equivalent to c = c * a /= Divide AND Assign c /= a is equivalent to c = c / a %= Modulus AND Assign c %= a is equivalent to c = c % a **= Exponent AND Assign c **= a is equivalent to c = c ** a //= Floor Division Assign c //= a is equivalent to c = c // a

Example 4: Example 4: Assignment_Operators.py a=2 b=3 c=0 print "a=",a," b=",b, " c=", c c = a + b print "c = a + b c += a print "c += a c *= a print "c *= a c /= a print "c /= a c = 3 c %= a print "c = 3,c %= a c **= a print "c **= a c //= a print "c //= a Output of Example 4 (Assignment_Operators.py): ) shows: Comments: text contained within comments is ignored by the Python interpreter. The # symbol begins comment in the source code. The comment is in effect until the end of the line of code:

Python Strings: Strings in Python are identified as a contiguous set of characters represented in the quotation marks either pairs of single or double quotes. Subsets of strings can be taken using the slice operator ([ ] and [:]) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end So ([-1] represent last index). The plus (+) sign is the string concatenation operator. The asterisk (*) is the repetition operator. For example: Example 5: String_Python.py str = 'Python Lab ' print str # Prints complete string print str[0] # Prints first character of the string print str[2: 5] # Prints characters starting from 3rd to 5th print str[2:] # Prints string starting from 3rd character print str * 2 # Prints string two times print str + "Exam" # Prints concatenated string print "length str ",len(str) Output of Example 5 (String_Python.py): Conversion functions: int() : convert string containing an integer number to an integer float() :convert string containing a floating point number to a floating point number a = "34" a = "3.4" print "data type a ",type(a) print "data type a ",type(a) x = int (a) x = float (a) print "data type x ",type(x) print "data type x ",type(x) Output:

User Input: The print function enables a Python program to display textual information to the user. Programs may use the input function to obtain information from the user. For example: input function assign entered value to x x = input () Examples: Example 6: usinginput.py x = input ("Please enter your name: ") print "Text entered: ", x print " Type: ", type(x) Output of Example 6 (String_Python.py): Example 7: usinginput.py x = input ("Please enter an integer value: ") y = input("please enter an integer value: ") print x, ' +', y, ' =', x + y Output of Example 7 (usinginput.py):

Work lab: Write program that converts a Fahrenheit degree to Celsius using the formula Celsius = 5 (fahrenheit 32) 9 Example 8: Convert Fahrenheit into Celsius fahrenheit= input("enter a degree in Fahrenheit:") Celsius=(5.0/9)*(fahrenheit-32) print "Fahrenheit", fahrenheit, " is ", Celsius, " in Celsius" Output of Example 8:

Exercises: 1) What happens if you attempt to use a variable within a program, and that variable has not been assigned a value? 2) Classify each of the following as either a legal or illegal Python identifier: a) Salim b) If c) 2x d) -4 e) sum_total f) sumtotal g) sum-total h) sum total i) public j) $16 k) _4 l) m) a27834 n) wilma s 3) Write an application to convert centimeters (input) to feet and inches (output). 1 inch= 2.54 cm 1 foot =30.48 cm 4) Write a Python program that split up a given number of seconds to hours, minutes, and seconds For example: If the user enters 10000 seconds, the program prints 2 hr, 46 min, 40 sec.