Programing and Problem Solving Mr. P.S. Desai Course Objective Prime objective is to give students a basic introduction to programming and problem solving with computer language Python. And to introduce students not merely to the coding of computer programs, but to computational thinking, the methodology of computer programming, and the principles of good program design including modularity and encapsulation. Course Objective 1.To understand problem solving, problem solving aspects, programming and to know about various program design tools. 2.To learn problem solving with computers 3.To learn basics, features and future of Python programming. 4.To acquaint with data types, input output statements, decision making, looping and functions in Python 5.To learn features of Object Oriented Programming using Python 6.To acquaint with the use and benefits of files handling in Python Course Outcomes: On completion of the course, learner will be able to– Co1: inculcate and apply various skills in problem solving. CO2: Choose most appropriate programming constructs and features to solve the problems in diversified domains. CO3: Exhibit the programming skills for the problems those require the writing of well documented programs including use of the logical constructs of language, Python. CO4: Demonstrate significant experience with the Python program development environment Unit I Problem Solving , Programming and Python Programming Part A: General Problem Solving Concepts : 1. 2. 3. 4. 5. 6. 7. Problem Solving in everyday life Types of Problem Problem solving with computer Difficulties with problem solving Problem Solving Aspects Top - Down Design Problem Solving Strategies Unit I Problem Solving , Programming and Python Programming Part B: Program Design tools 1. 2. 3. 4. Algorithm Flowchart Pseudo -codes Implementation of Algorithm Unit I Problem Solving , Programming and Python Programming Part C: Python Programming : 1. 2. 3. 4. 5. 6. 7. 8. Features of Python History and Future of Python Writing and executing Python program Literal constant , Variable and identifiers Data types Comments , Reserved words , Indentation Operator and Expression Input Operation General Problem Solving Concepts : Problem Solving in everyday life What is Problem ? 1. 2. A Problem is a state of difficulty that need to be resolved While solving a problem there is a desire to attain some specific goal . Here is some examples we face in day to day life can be important or can be least importance . 1. 2. 3. 4. Will I get proper transport to go to my workspace ? Should I wear shoes today ? Should I go to a movies ? Which Mobile phone should I buy? General Problem Solving Concepts : Six steps of Problem Solving 1. Define the Problem 2. Determine the Root Cause(s) of the Problem 3. Develop Alternative Solutions 4. Select a Solution 5. Implement the Solution 6. Evaluate the Outcome Six steps of Problem Solving Step -1 Step-2 Step-3 Step-4 Step-5 Step-6 Types of Problem 1. Problem Based on algorithmic solution : Sequence of instruction . For solving some problem , series of actions are taken to reach the solution. 2. Problem based on heuristic Solutions : The Solution that can not reached through direct set of steps are called heuristic solutions . With heuristic solution problem solver has to follow six steps of problem solving for more than once . Problem solving with computer Solution : In is the instruction that must be followed to produce the beat result . Results: It is an outcome or the completed computer -assisted answer . Program : Computer program are set of instructions executed to obtain solution to certain problem . These programs are written in some specific programming language. Computer deal with the problem having algorithmic solution . The field of computer that deal with solving the heuristic problems is called artificial intelligence . Difficulties with problem solving 1. 2. 3. 4. 5. We do not understand the problem correctly . Sometimes we do not define the problem correctly or adequately People get afraid of taking decisions while solving the problems Sometimes thee list of alternatives is incomplete . The sequence of solutions to the problems is not logical many times . 6. When solving problem on the computer , one of the most difficult tasks for the problem solver is writing the instructions . Problem Solving Aspects Mainly focus on 1. 2. 3. 4. 5. 6. Problem Definition phase Problem Solving Phase Use of specific Example Similarities among problems Working backwards from the solution General Problem solving strategies Top - Down Design Top- Down approach is essentially the breaking down of a system to gain insight into the subsystem that make it up. An overview of the system is formulated . Each subsystem is then refined in yet greater detail, sometimes in many additional subsystem levels , until the entire specification is reduced top base elements . The top design strategy is also known as stepwise refinement . Top - Down Design Problem Solving Strategies 1. 2. 3. 4. 5. Requirement Analysis Desgin Implementation Testing Software Deployment , training and Support Part B: Program Design tools:Algorithm 1. 2. 3. 4. 5. 6. Algorithm gives the logic of the program , that is , a step- by -step description of how to arrive at a solution . An Algorithm provides a blueprint to writing a program to solve a particular problem . It is considered to be an effective procedure for solving a problem in a finite number of steps . That is , a well defined algorithm always provides an answer , and is guaranteed to terminate . Algorithms are mainly used to achieve software reuse . Ones we have an idea or a blueprint of a solution , we can implement it in any language such as C ,C++, Python, JAVA and so on. Different Approaches to Designing an Algorithm Algorithm are used to manipulate the data for given problem . For a complex problem, its algorithm is often divided into smaller units called modules . This process of dividing an algorithm into modules is called modularization . There are Two approaches to design an algorithm 1. 2. Top-Down Approach Bottom- up approach Control structure used in Algorithm An algorithm has a finite number of steps and some steps may involve decision making and repetition . Broadly speaking an algorithm may employ three control structures, namely Sequence , Decision , Repetition. Sequence : Sequence means that each step of algorithm is executed in the specific order. Decision : Decision statement are used when the outcome of the process depends on some condition . Repetition : Which involves executing one or more steps for a number of times . Examples of Algorithm 1. Write an algorithm for swapping two number 2. Write an algorithm to find the larger of two number 3. Write a algorithm that number is even or odd Flowchart A flowchart is a graphical or symbolic representation of a process . Flowchart is pictorial view of algorithm When designing a flowchart , each step in the process is depicted by different symbol and is associate with short symbol . Flowchart Symbol Examples of Flowchart 1. 2. 3. 4. Draw a flowchart to calculate the sum of first 10 natural number Draw a flowchart to add two number Draw a flowchart to calculate the salary of a daily wager Draw a flowchart to determine the largest of three numbers Pseudocodes Pseudocodes is a compact and informal high level description of an algorithm that uses the structural conventional of a programming languages. Pseudocode must be complete , describing the entire logic of the algorithm , so that can be translated strainghway into a programming languages. Pseudocode are an outline of a program that can easily be converted into programing statements . Pseudo Codes examples 1. Write a psudocode for calculating the price of product after adding the sales tax to its original price . Part C: Basics of Python Programming Python has been consecutively topping the most loved programming language on the StackOverflow developers survey report. (Ref. https://data-flair.training/blo gs/python-career-opportunit ies/) Growth of Python Importance of Python ●Python is used in Data Science ●Python used with Big Data ●Python used in Artificial Intelligent ●Python used in Scripting and automation Job Profile in Python Python Web Developer Software Engineer Data Analyst Data Scientist Machine Learning Engineers DevOps Engineer Research Analyst Product Manager Features of Python 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. Simple Easy to learn Free and open source High level language Intrative Portable Object oriented Embeddable Secure Dynamic History of Python : Inventor Guido Van Rossum Interview: Guido van Rossum: The Early Years of Python Coursera Interview Link of Guido Van Rossum : Click Interview Reference : https://www.coursera.org/ Why is it called ‘Python’? Monty Python’s Flying circus ( a BBC Comedy series from the 70’s) Version details 1. 2. 3. 4. 5. Version 1.0 was released in 1991. Version 2.0 was released in 2000 . Python 2.7 last release of 2.0 version. Python 3.0 was relased in 2008. Current version is 3.6.4. Interview: Guido van Rossum - The Modern Era of Python Coursera Interview Link of Guido Van Rossum : Click Interview Reference : https://www.coursera.org/ Application of Python 1. 2. 3. 4. 5. 6. 7. Embedded Scripting Language GUI-based desktop application Image processing and Graphic design application Games Enterprise and Business application Operating System Language Development Writing and Executing First Python Program 1. 2. 3. 4. Open an editor Write the instruction Save it as a file with the filename having the extension .py Run the interpreter with the filename having extension .py Example print(“Hello all welcome to Python world of Joy”) Building Blocks of Python Programming Language 1. 2. 3. 4. 5. 6. 7. 8. Constants Variables Identifiers Data Types Reserved words Indentation Operators Expressions Constants Fix values such as numbers , letters and strings are called constsant because their values does not change . print(576) print(5.6) print(“Hello World”) big part of any programming language is the syntax for constants. So, like a 576 or 5.6, numbers both integer and floating point numbers. We've been using this on calculators. They make a lot of sense to us. The kind of constants that are a little bit different are things like string constants. So "Hello world" is a string constant. We use that so that our program can say nice things to people that are using our program. And so, we call them constants because they don't change but it's kind of obvious, constants. Variable and Identifiers Using just constants you cannot do much in your program .For developing little complex program , you need to store information and manipulate it as required .THis is where variables can help. Variables are reserved memory locations that stores values. Identifiers are names given to something . This somethings can be a variable , function, class , modules or objects. Choice variable name sensible. Python Variable Name Rules Must start with letter or (_) underscore . Must consist of letter , number and _ underscore . Punctuation characters such as @ , $ , % not allowed within names. Case sensitive Good: sum, _My_var , num1 , r, var_20 Bad: 1num , %check , Basic sal , H#R&A Difference : check , Check , CHECK Data Types Variables can hold values of different types called data types . We need different data types to store different types of values in the variables . Based on the data type of a variable, the interpreter reserves the memory for it and also determines the data that can be stored in the reserved memory . Data types supported by Python 1. 2. 3. 4. 5. Numbers String List Tuple Dictionary Data types with example Num-7 # number: integer amt=7.5 #number : Floating code=’a’ #String pi=3.1415926536 msg=”Hi” # number : Floating # String Reserved Words You cannot use reserved words as variable names/identifiers . Certain words which have a predefined meaning. Indentation Whitespace at the beginning of the line called indentation Example Age =7 price=10 count=6 Indentation These whitespaces are very important in python In most programming languages , indentation has no effect on program logic. In python indentation has a major effect on program logic. In python , indentation is used to associate and group statements . Operators and Expression x=2 x=x+2 print(x) Operators and Expression x1q3z9ocd=35.0 x1q3z9afd=12.50 x1q3p9afd=x1q3z9ocd*x1q3z9afd a=35.0 b=12.50 c=a*b print(c) hours=35.0 rate=12.50 pay=hours*rate print(pay) Variable name and expression Types of Operator 1. 2. 3. 4. 5. 6. 7. Arithmetic operators Assignment operators Comparison operators Logical operators Membership operators Identity operators Bitwise operators Arithmetic operators Assignment operators Comparison operators Logical operators Identity operators Membership operators Bitwise operators Operator Precedence and Associativity Program Examples