Python The way of a program Srinidhi H Asst Professor Dept of CSE, MSRIT 1
Problem Solving Problem solving means the ability to formulate problems, think creatively about solutions, and express a solution clearly and accurately. 2
The Python programming language High-level language. The engine that translates and runs Python is called the Python Interpreter. Immediate mode Script mode 3
What is a program? A program is a sequence of instructions that specifies how to perform a computation. input Get data from the keyboard, a file, or some other device. output Display data on the screen or send data to a file or other device. math Perform basic mathematical operations like addition and multiplication. conditional execution Check for certain conditions and execute the appropriate sequence ofstatements. repetition Perform some action repeatedly, usually with some variation. 4
What is debugging? Programming errors are called bugs and the process of tracking them down and correcting them is called debugging. Syntax errors Syntax refers to the structure of a program and the rules about that structure. Python will display an error message and quit, and you will not be able to run your program. Semantic errors Runtime errors If there is a semantic error in your program, it will run successfully, in the sense that the computer will not generate any error messages, but it will not do the right thing. Error does not appear until you run the program. These errors are also called exceptions because they usually indicate that something exceptional (and bad) has happened. 5
The first program Install Python Install pyscripter https://www.youtube.com/watch?v=1hz38rzykue 6
VARIABLES, EXPRESSIONS AND STATEMENTS 7
Values and data types A value is one of the fundamental things like a letter or a number that a program manipulates. Values are classified into different classes, or data types: 4 is an integer, and "Hello, World!" is a string, Python has a function called type which can tell you. 8
Values and data types examples >>> type("hello, World!") <class str > >>> type(17) <class int > >>> type("17") <class str > >>> type("3.2") <class str > >>> type(3.2) <class float > >>> type( This is a string. ) <class str > >>> type("and so is this.") <class str > >>> type("""and this.""") <class str > >>> type( and even this... ) <class str > 9
Self -test How many lines of screen output is displayed by the following, print('apple\nbanana\ncherry\npeach') (a) 1 (b) 2 (c) 3 (d) 4 Which of the following are valid string literals in Python. (a) "Hello" (b) 'hello' (c) "Hello' (d) 'Hello there' (e) '' 10
Variables A variable is a name that is associated with a value. The assignment operator = is used to assign values to variables. The id function produces a unique number identifying a specifi c value (object) in memory 11
Variables example 12
Keywords and Statements A statement is an instruction that the Python interpreter can execute. Ex: if, else, import 13
Evaluating expressions An expression is a combination of values, variables, operators, and calls to functions. >>> 1 + 1 2 >>> len("hello") 5 14
Operators and operands Operators are special tokens that represent computations like addition, multiplication and division. The values the operator uses are called operands. The tokens +, -, and *, and the use of parenthesis for grouping, mean in Python what they mean in mathematics. The asterisk (*) is the token for multiplication, and ** is the token for exponentiation. 15
Type converter functions >>> int(3.14) 3 >>> float(17) 17.0 >>> float("123.45") 123.45 >>> str(17) 17 >>> str(123.45) 123.45 16
Operations on strings Interestingly, the + operator does work with strings, but for strings, the + operator represents concatenation, not addition. The * operator also works on strings; it performs repetition. 1 fruit = "banana" 2 baked_good = " nut bread" 3 print(fruit + baked_good) Fun *3 is FunFunFun. The output of this program is banana nut bread. 17
format The built-in format function can be used to produce a numeric string of a given fl oating-point value rounded to a specifi c number of decimal places.... format(13402.25, ',.2f') 13,402.24 18
Input There is a built-in function in Python for getting input from the user: n = input("please enter your name: ") 19
Let s try a simple program P1. Write a Python program that prompts the user for two floating-point values and displays the result of the first number divided by the second, with exactly six decimal digits of precision displayed. print("{:.6f}".format(c)) P2. Calculate the area of a circle 20
Let s try a simple program P5. Write a Python program that allows the user to enter two integer values, and displays the results when each of the following arithmetic operators are applied. For example, if the user enters the values 7 and 5, the output would be, All floating-point results should be displayed with two decimal places of accuracy. In addition, all values should be displayed with commas where appropriate. 21
CONDITIONALS 22
Boolean values and expressions A Boolean value is either true or false. In Python, the two Boolean values are True and False. >>> type(true) <class bool > >>> type(true) Traceback (most recent call last): File "<interactive input>", line 1, in <module> NameError: name true is not defined 23
Boolean values and expressions A Boolean expression is an expression that evaluates to produce a result which is a Boolean value. 24
Conditional execution The simplest form is the if statement: 25
Conditional execution The simplest form is the if statement: 26
Chained conditionals 27
Nested conditionals 28