Full Transcript

Introduction to Programming in Python (IT1002) Lecture - 3 Data and Expressions: Literals, Variables and Identifiers Dr. Rabindra Prasad (Course Instructor) Assistant Professor Department of Mechanical Engineering ...

Introduction to Programming in Python (IT1002) Lecture - 3 Data and Expressions: Literals, Variables and Identifiers Dr. Rabindra Prasad (Course Instructor) Assistant Professor Department of Mechanical Engineering Today’s Outline Previous Session: Introduction to Basics of computer science, Hardware and Software. Computational problem solving and introduction to Programming Languages. Today’s Session: Basic Elements of Python Programs Literals, Assignments. Datatype Conversion. Identifiers, and Expressions. Hands on Session with Jupyter Notebook or VS Code : We will practice on the Python basic elements in Jupyter Notebook or VS Code. 2 Learning a Language Learning English: Paragraph Alphabets Word Sentence (Hello I am Good. (a, b, c, d,..) (Hello, Good, etc.) (Hello I am Good.) I want to learn C.) Learning Python: Literals/Character Tokens, Keyword, Instruction or Program Set tokens, etc. Statement (a=10 (a, b, c, d,.., +,-, *, /, (if, def, 10, etc.) ( a=10, print(a) ) b=a+20) %, #, >, >> print(“PDPM IIITDM") PDPM IIITDM >>> print(“Welcome to IIITDM") Welcome to IIITDM >>> print(3) 3 >>> print(2.3) 2.3 6 Literals String Literals: A string literal is a sequence of characters surrounded by quotes. We can use both single, double or triple quotes for a string. And, a character literal String is a single character surrounded by single or double quotes. Numeric Numeric Literals: Literals Numeric Literals are immutable (unchangeable). Numeric literals can Boolean belong to 3 different numerical types Integer, Float, and Complex. Boolean Literals: Collection A Boolean literal can have any of the two values: True or False. Special Collection literals: There are four different literal collections List literals, Tuple literals, Dict literals, and Set literals. Special literals: Python contains one special literal i.e. None. We use it to specify to that field that is not created. 7 Statements Python statements are nothing but logical instructions that interpreter can read and execute. It can be both single and multiline. There are two categories of statements in Python: Value Based Statements Simple Variable Based Assignments Multiple Operation based Expression 8 Simple Assignment Statements A literal is used to indicate a specific value, which can be assigned to a variable >>> x = 2  x is a variable and 2 is its value >>> print(x) 2 >>> x = 2.3 >>> print(x) 2.3 9 Simple Assignment Statements A literal is used to indicate a specific value, which can be assigned to a variable >>> x = 2  x is a variable and 2 is its value >>> print(x) 2  x can be assigned different values; >>> x = 2.3 hence, it is called a variable >>> print(x) 2.3 10 Simple Assignment Statements: What we Think A simple way to view the effect of an assignment is to assume that when a variable changes, its old value is replaced >>> x = 2 x = 2.3 Before After >>> print(x) 2 x 2 x 2.3 >>> x = 2.3 >>> print(x) 2.3 11 Simple Assignment Statements: What Actually Happen Python assignment statements are slightly different from the “variable as a box” model In Python, values may end up anywhere in memory, and variables are used to refer to them x = 2.3 >>> x = 2 After Before >>> print(x) What will 2 2 happen to x 2 x >>> x = 2.3 value 2? >>> print(x) 2.3 2.3 12 Garbage Collection Interestingly, as a Python programmer you do not have to worry about computer memory getting filled up with old values when new values are assigned to variables After Memory location Python will automatically clear old values out of memory in a process x 2 X will be automatically reclaimed by the known as garbage collection garbage collector 2.3 13 Simultaneous Assignment Python allows us also to assign multiple values to multiple variables all at the same time >>> x, y = 2, 3 >>> x 2 >>> y 3 >>> This form of assignment might seem strange at first, but it can prove remarkably useful (e.g., for swapping values) 14 Simultaneous Assignment Suppose you have two variables x and y, and you want to swap their values (i.e., you want the value stored in x to be in y and vice versa) >>> x = 2 >>> y = 3 >>> x = y >>> >>> y x = x X CANNOT be done with two simple assignments 3 >>> y 3 15 Simultaneous Assignment Suppose you have two variables x and y, and you want to swap their values (i.e., you want the value stored in x to be in y and vice versa) >>> x = 2 Thus far, we have been using >>> y = 3 different names for >>> temp = x CAN be done with variables. These names >>> x = y three simple assignments, are technically called identifiers >>> y = temp >>> x  but more efficiently with simultaneous assignment 3 >>> y 2 >>> x,y = y,x 16 Identifiers Python has some rules about how identifiers can be formed Every identifier must begin with a letter or underscore, which may be followed by any sequence of letters, digits, or underscores >>> x1 = 10 >>> x2 = 20 >>> y_effect = 1.5 >>> celsius = 32 >>> 2celsius File "", line 1 2celsius ^ SyntaxError: invalid syntax 17 Identifiers Python has some rules about how identifiers can be formed Identifiers are case-sensitive >>> x = 10 >>> X = 5.7 >>> print(x) 10 >>> print(X) 5.7 18 Identifiers Python has some rules about how identifiers can be formed Some identifiers are part of Python itself (they are called reserved words or keywords) and cannot be used by programmers as ordinary identifiers False class finally is return None continue for lambda try True def from nonlocal while and del global not with as elif if or yield assert else import pass break except in raise Python Keywords 19 Identifiers Python has some rules about how identifiers can be formed Some identifiers are part of Python itself (they are called reserved words or keywords) and cannot be used by programmers as ordinary identifiers >>> for = 4 File "", line 1 An example… for = 4 ^ SyntaxError: invalid syntax 20 Expressions You can produce new data (numeric or text) values in your program using expressions >>> x = 2 + 3  This is an expression that uses the >>> print(x) addition operator 5 >>> print(5 * 7) 35 >>> print("5" + "7") 57 21 Expressions You can produce new data (numeric or text) values in your program using expressions >>> x = 2 + 3  This is an expression that uses the >>> print(x) addition operator 5 >>> print(5 * 7)  This is another expression that uses the 35 multiplication operator >>> print("5" + "7") 57 22 Expressions You can produce new data (numeric or text) values in your program using expressions >>> x = 2 + 3  This is an expression that uses the >>> print(x) addition operator 5 >>> print(5 * 7)  This is another expression that uses the 35 multiplication operator >>> print("5" + "7") 57  This is yet another expression that uses the addition operator but to concatenate (or glue) strings together 23 Expressions You can produce new data (numeric or text) values in your program using expressions >>> x = 6 >>> print(x*y) >>> y = 2 12 >>> print(x - y) >>> print(x**y) Another 4 Yet another 36 example… >>> print(x/y) example… >>> print(x%y) 3.0 0 >>> print(x//y) >>> print(abs(-x)) 3 6 24 Expressions: Summary of Operators Operator Operation + Addition - Subtraction * Multiplication / Float Division ** Exponentiation abs() Absolute Value // Integer Division % Remainder Python Built-In Numeric Operations 25 Script One problem with entering code interactively into a Python shell is that the definitions are lost when we quit the shell If we want to use these definitions again, we have to type them all over again! To this end, programs are usually created by typing definitions into a separate file called a module or script This file is saved on disk so that it can be used over and over again A Python module file is just a text file with a.py extension, which can be created using any program for editing text (e.g., notepad or vim) 26 Programming Environments and IDLE A special type of software known as a programming environment simplifies the process of creating modules/programs. A programming environment helps programmers write programs and includes features such as automatic indenting, color highlighting, and interactive development. The standard Python distribution includes a programming environment called IDLE that you can use for working on the programs of this course. 27 Summary Programs are composed of statements that are built from identifiers and expressions Identifiers are names They begin with an underscore or letter which can be followed by a combination of letter, digit, and/or underscore characters They are case sensitive Expressions are the fragments of a program that produce data They can be composed of literals, variables, and operators 28 Summary A literal is a representation of a specific value (e.g., 3 is a literal representing the number three) A variable is an identifier that stores a value, which can change (hence, the name variable) Operators are used to form and combine expressions into more complex expressions (e.g., the expression x + 3 * y combines two expressions together using the + and * operators) 29 Summary In Python, assignment of a value to a variable is done using the equal sign (i.e., =) Using assignments, programs can get inputs from users and manipulate them internally Python allows simultaneous assignments, which are useful for swapping values of variables 30 Thank You ? 31

Use Quizgecko on...
Browser
Browser