Introduction to Python 3, Variables and Expressions Difficulties : Two Things at Once 1. First you must solve the problem. When solving a problem, I’d suggest being as far away from a computer as possible. Think about how you would solve the problem yourself and write down those steps. 2. Once you have those basic steps down, you can translate those into something that that the computer can understand. What is a good Program? ● Solves the problem. ● It should be easily readable. ● It should contain comments for anything non obvious. The Three Rules ● ● ● Rule 1: Think before you program Rule 2: A program is a human-readable essay on problem solving that also happens to execute on a computer Rule 3: The best way to improve your programming and problem solving skills is to practice. Types of Computer Languages ● Machine Language - Punch cards, switches. ● Machine language is exactly what the computer understands. It’s all 1’s and zero’s. ● Higher-Level Languages C++, C, Java, Python, C#, Javascript Python! Who uses Python? ● ● ● ● ● ● ● ● Google Yahoo Dropbox Instagram Facebook Netflix Disney and a bunch more Why Python ● Features ○ Low cognitive load for students. Very high level English-like language. ○ Easy to apply to problems. You don’t have to write dozens of lines of code to accomplish simple tasks. ○ Broad support. Can be used by different professionals in different settings on different OS’s. ● Open Source ○ Community supported ○ Multi-platform ○ Free Programming Using Python • Python interpreter: is a computer program that executes code written in the Python programming language. • An interactive interpreter: is a program that allows the user to execute one line of code at a time. • The interactive interpreter displays a prompt (">>>") that indicates the interpreter is ready to accept code. Executing a Python Program • The Python interactive interpreter is useful for simple operations or programs consisting of only a few lines. • Programmer can write Python code in a file, and then provide that file to the interpreter. The interpreter begins by executing the first line of code at the top of the file, and continues until the end is reached. • Statement: is a program instruction. A program mostly consists of a series of statements. • Expressions: are code that return a value when evaluated. • A new variable is created by performing an assignment using the = symbol. • print() :displays variables or expression values. • '#' characters denote comments. Computer Program Basics • Program consists of instructions executing one at a time. • Basic instruction types: – Input: program gets data from file, keyboard, network. – Process: program performs computations on that data ( add, subtract,…etc). – Output: program puts that data somewhere ( file, screen, network,.. etc). • Note: • Programs use variables to refer to data – – – – First character must be _ or A-Z, a-z. Any remaining characters can be 0-9, _, A-Z or a-z Can not be a keyword It is case sensitive. • A sequence of instructions that solves a problem is called an algorithm Basic Input and Output • input() function: – It causes the interpreter to wait until the user enters some text(numbers, letters, special characters) and has pushed the return key. – It will read text entered by the user, and assign the entered text to a variable. Ex: name=input() - Reading from input always results in a string type. However sometimes we need to input integers or decimal numbers (int(),float()) Ex: my_int = int('123') miles=float(input('Enter distance in miles')) Ex:salary = int(input()) You can combine input() and int() to read in a string from the user and then convert that string to an integer. Basic Input and Output • Print() functions: • It is used to print output to screen. • Each print() will output on a new line. Ex: print('hello world') A string literal can be surrounded by matching single or double quotes. Note: print('Hello', end=' ') print('CS101', end=' ') Specifying end=' ', keeps the next print's output on the same line separated by a single space. Output: Hello CS101 • • Printing multiple items using a single print statement: salary=1000 Print('Salary is: ',salary) # Comma separates multiple items and each item in the output will be separated by a space Output: Salary is: 1000 Newline characters: – print('Hello\nCS101') #(\n moves output to new line) output: Hello CS101 - Using print() by itself without any text also prints a single newline. Errors • Syntax Error: The program contains invalid code that can not be understood. • Indentation Error: The lines of the program are not properly indented. • ValueError: An invalid value is used. Ex: giving letters to an int() • NameError: The program tries to use a variable that does not exist. • TypeError: An operation uses incorrect types. (adding an int to a string) • Logic Errors: Known as bug. Example wage = 20 hours = 40 weeks = 50 salary = wage * hours * weeks print('Salary is:', salary) hours = 35 salary = wage * hours * weeks print('New salary is:', salary) Output: Salary is: 40000 New salary is: 35000