G. Pullaiah College of Engineering and Technology

Slides:



Advertisements
Similar presentations
What type of data can a variable hold?
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Intro to Python Paul Martin. History Designed by Guido van Rossum Goal: “Combine remarkable power with very clear syntax” Very popular in science labs.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
4. Python - Basic Operators
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Python Control Flow statements There are three control flow statements in Python - if, for and while.
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Input, Output, and Processing
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
7 - Programming 7J, K, L, M, N, O – Handling Data.
Python Basics.
String and Lists Dr. José M. Reyes Álamo.
Topics Designing a Program Input, Processing, and Output
Python Variable Types.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Visual Basic 6 (VB6) Data Types, And Operators
2. Java language basics (2)
CS-104 Final Exam Review Victor Norman.
Multiple variables can be created in one declaration
Presented By S.Yamuna AP/IT
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Arithmetic operations, decisions and looping
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Lecture 2 Python Programming & Data Types
Basics of ‘C’.
Numbers.
Chapter (3) - Looping Questions.
String and Lists Dr. José M. Reyes Álamo.
Introduction to Programming Using Python PART 2
Lecture 2 Python Programming & Data Types
Basics.
Computer Science Core Concepts
Core Objects, Variables, Input, and Output
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Introduction to Computer Science
Class code for pythonroom.com cchsp2cs
More Basics of Python Common types of data we will work with
Problem 1 Given n, calculate 2n
LOOP Basics.
COMPUTING.
Introduction to PYTHON
Selamat Datang di “Programming Essentials in Python”
Presentation transcript:

G. Pullaiah College of Engineering and Technology Python Programming Department of Computer Science & Engineering

Unit- 2 Types - Integers, Strings, Booleans; Operators- Arithmetic Operators, Comparison (Relational) Operators, Assignment Operators, Logical Operators, Bitwise Operators, Membership Operators, Identity Operators, Expressions and order of evaluations Control Flow- If, if-elif-else, for, while break, continue, pass

Types Python has various standard data types Python has three standard data types: Numbers Strings Boolean

1. Numbers Python has 4 built-in numeric data types: Integers Long integers Floating point numbers Imaginary numbers

1. Integers Integers are whole numbers They are 32- bit numbers Range: -2147483648 to 2147483647 By default integers are base-10 numbers >>>300 #300 in decimal 300 >>>0x12c #300 in hex >>>0o454 #300 in octal

2. Long integers Similar to integers, except that the maximum and minimum values of long integers are restricted by how much memory you have. To differentiate between the two types of integers append an “L” to the end of long integers. (lowercase –l) >>>200L 200L

3. Floating point numbers: Represent fractional numeric values such as3.14159 They can represented in exponent form They are rounded values Ex: 200.05 9.80655 .1 2005e-2 6.0221367E23

4. Imaginary numbers We form an imaginary number by appending a “j” to the decimal number( int or float) >>>2+5j (2+5j) >>>2*(2+5j) 4+10j

2. Strings A contiguous set of characters represented in the quotation marks. Python allows for 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. The plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition operator

>>> c='aaaaaaaa bbbb cccc dddd eee ffff' >>> c 'aaaaaaaa bbbb cccc dddd eee ffff‘ >>> st2="aaaaaaaa bbbb ccccc dddd eee ffff" >>> st2 'aaaaaaaa bbbb ccccc dddd eee ffff‘

>>> st3="""aaaaa bbbbb ccccc ddddd eeeee fffff""“ >>> st3 'aaaaa bbbbb\nccccc ddddd\neeeee fffff' >>>

str = 'Hello World!' print(str) print(str[0]) print(str[2:5]) print(str + "TEST") Output: Hello World! H llo llo World! Hello World!Hello World! Hello World!TEST

3. Boolean The identifiers True and False are interpreted as Boolean values with the integer values of 0 and 1, respectively. In case of if, while – non zero values ->True Zero values -> False >>> True True >>> False False

>>> a=True >>> a True >>> 2>3 False >>> 2<7

Operators- Arithmetic Operators, Comparison (Relational) Operators, Assignment Operators, Logical Operators, Bitwise Operators, Membership Operators, Identity Operators,

1. Arithmetic Operators Operators + Addition - Subtraction * Multiplication / Division % Modulus ** Exponent // integer division Example: python8.py

2. Relational Operators Operators == != <> > < >= <=

a = 21 b = 10 if ( a == b ): print "Line 1 - a is equal to b" else: print "Line 1 - a is not equal to b"

3. Assignment Operators

If Statement if expression: statement(s) Example: var1 = 100 if var1: print ("1 - Got a true expression value" ) print var1 Print( "Good bye!" )

If…else Statement if expression: statement(s) else:

var1 = 100 if var1: print "1 - Got a true expression value" print var1 else: print "1 - Got a false expression value"

elif Statement if expression1: statement(s) elif expression2: elif expression3: else:

var = 100 if var == 200: print ("1 - Got a true expression value" ) print var elif var == 150: print ("2 - Got a true expression value" ) elif var == 100: print ("3 - Got a true expression value" ) else: print ("4 - Got a false expression value" ) print ("Good bye!" )

Single Statement Suites var = 100 if ( var == 100 ) : print ("Value of expression is 100“) print("Good bye!" ) Output: Value of expression is 100 Good bye!

While Loop while expression: statement(s) Example: count = 0 while (count < 9): print ('The count is:', count ) count = count + 1 print "Good bye!"

Using else Statement with Loops Python supports to have an else statement associated with a loop statement. If the else statement is used with a for loop, the else statement is executed when the loop has exhausted iterating the list. If the else statement is used with a while loop, the else statement is executed when the condition becomes false.

count = 0 while count < 5: print (count, " is less than 5" ) count = count + 1 else: print( count, " is not less than 5“)

0 is less than 5 1 is less than 5 2 is less than 5 3 is less than 5 4 is less than 5 5 is not less than 5

Single Statement Suites flag = 1 while (flag): print ('Given flag is really true!' ) print( "Good bye!" )

For Loop for iterating_var in sequence: statements(s)

Ex-1 for letter in 'Python': print( 'Current Letter :', letter ) Output: Current Letter : P Current Letter : y Current Letter : t Current Letter : h Current Letter : o Current Letter : n

Ex-2 fruits = ['banana', 'apple', 'mango'] for fruit in fruits: print( 'Current fruit :', fruit ) print "Good bye!" Output: Current fruit : banana Current fruit : apple Current fruit : mango Good bye!

Break Statement It terminates the current loop and resumes execution at the next statement The break statement can be used in both while and for loops In nested loops, the break statement stops the execution of the innermost loop and start executing the next line of code after the block. Syntax: break

for letter in 'Python': # First Example if letter == 'h': break print 'Current Letter :', letter Output: Current Letter : P Current Letter : y Current Letter : t

var = 10 while var > 0: print 'Current variable value :', var var = var -1 if var == 5: break

Output: Current variable value : 10 Current variable value : 9 Current variable value : 8 Current variable value : 7 Current variable value : 6

Continue Statement It returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop. The continue statement can be used in both while and for loops. Syntax continue

for letter in 'Python‘: if letter == 'h': continue print 'Current Letter :', letter Output: Current Letter : P Current Letter : y Current Letter : t Current Letter : o Current Letter : n

var = 10 while var > 0: var = var -1 if var == 5: continue print 'Current variable value :', var

Current variable value : 9 Current variable value : 8 Current variable value : 7 Current variable value : 6 Current variable value : 4 Current variable value : 3 Current variable value : 2 Current variable value : 1 Current variable value : 0

Pass Statement It is used when a statement is required syntactically but you do not want any command or code to execute. The pass statement is a null operation; nothing happens when it executes. Syntax pass

i=1 while(i<=10): if(i==6): pass else: print(i) i=i+1 Output: 1 2 3 4 5 7 8 9 10

i=1 while(i<=10): if(i==6): print("hello") pass else: print(i) i=i+1

1 2 3 4 5 hello 7 8 9 10

i=1 while(i<=10): if(i==6): print("hello") continue else: print(i) i=i+1 Output Infinite loop

i=1 while(i<=10): if(i==6): continue else: print(i) i=i+1

Output 1 2 3 4 5 Infinite (printing nothing)