Python 3
Welcome to Python 3 Some history Python was created in the late 1980s by Guido van Rossum In December 1989 is when it was implemented Python 3 was released in December of 2008 It is not backward compatible with Python 2 The Python developers use a simpler syntax than most other programming languages and wanted learning Python to be fun
The Python language is named for the Monty Python Flying Circus not the snake. Although many books have pictures of snakes on the front, my guess is because the authors don t wish to pay the royalties to Monty Python Flying Circus to use it s picture on the cover
The Programming Language Like all languages, Python has a grammar or a syntax. Syntax is the grammar, structure, or order of the elements in a language statement. i.e. blocks of code must be indented, the main program must begin at the left margin, etc i.e. print( the answer is, answer) NO Program can be written to solve an unsolvable problem
Syntax & Grammar Python is intended to be a highly readable language. It is designed to have an uncluttered visual layout; it frequently using English keywords where other languages use punctuation. Python requires less boilerplate than traditional manifestly typed structured languages such as C or Pascal, and has a smaller number of syntactic exceptions and special cases than either of these
Some terms Boilerplate In information technology, a boilerplate is a unit of writing that can be reused over and over without change Case Sensitive Python is a case sensitive language. That means when you name a variable or a module, a.k.a. function, the use all the upper/lowercase letters can change the name of the variable or module; for example the name FIRST is not the same as First, or FiRst, or first
More Syntax & Grammar The simplicity of Python is demonstrated by its version of the classic "Hello world" program: print("hello world") Indentation Python uses whitespace indentation, rather than curly braces or keywords, to delimit blocks; a feature also termed the off side rule. An increase in indentation comes after certain statements; a decrease in indentation signifies the end of the current block.
Hello World Program in Java class myfirstjavaprog { public static void main(string args[]) { System.out.println("Hello World!"); } }
Hello World in C++ // 'Hello World!' program #include <iostream> int main() { std::cout << "Hello World!" << std::endl; return 0; }
000100 IDENTIFICATION DIVISION. 000200 PROGRAM ID. HELLOWORLD. 000300 000400* 000500 ENVIRONMENT DIVISION. 000600 CONFIGURATION SECTION. 000700 SOURCE COMPUTER. RM COBOL. 000800 OBJECT COMPUTER. RM COBOL. 000900 001000 DATA DIVISION. 001100 FILE SECTION. 001200 100000 PROCEDURE DIVISION. 100100 100200 MAIN LOGIC SECTION. 100300 BEGIN. 100400 DISPLAY " " LINE 1 POSITION 1 ERASE EOS. 100500 DISPLAY "Hello world!" LINE 15 POSITION 10. 100600 STOP RUN. 100700 MAIN LOGIC EXIT..100800 EXIT. Hello World in Cobol
Errors Syntax error an error in the grammar of the language a typo, a wrong indent Logic error an error in the programming logic that is syntactically correct BUT produces the wrong answer. You enter the numbers 6 and 7 and are told the sum is 42 Runtime error an error that causes the program to stop unexpectedly. A number you are dividing by some how becomes 0 and the program crashes You attempt to find the square root of a negative number
Hello World BASIC 10 PRINT "Hello World!"
Programming
Variables A variable is a placeholder in the memory of a computer for data. In Python, variable names usually begin with a lowercase letter and are followed by other letters or the digits zero through nine. Usually, names beginning with capital letters or all capital letters are you reserved for constants. Names that begin with the underscore character are typically used to name modules that relate to some function that is used in Python, in other words system programs.
Examples Valid variable names: price, name, hours, pay, day1_value Invalid variable names: num books, name%, money$
Statements print comments input eval int float
print statement Used to produce output on the computer screen; print is a reserved word and a built in python module. The print statement can have a single item or multiple items to display in a single statement print( this is a string ) or print (c) print( The answer is,h) print( The three values are:,a, b, c)
Comments Comments add descriptions to a program that help the programmer remember what a particular line or lines of code do. Comments are also used to identify who wrote the code and when. # this is a comment in python Comments can begin at the left margin or occur at the end of a line Comments are for PEOPLE only; interpreters and compilers ignore them.
Type this example
input statements Get data from the screen by use of wired or wireless connections price = input( Enter cost of buying 3 items ) When this statement is executed, the value stored in price is assigned as a character to price.
Functions Used Convert In this case strings to numeric values They are: int, float, eval int() returns the integer value of a str. float() returns the real value of the str. eval() evaluates the funtion and determines ho it will work
Assignment statements In Python, like other computer languages, statements are read from left to right. The variable on the left side of the equation is the variable that will hold your answer. On the right side of the is the equation is what needs to be solved Example Payee = hours * wage;
Assignment statement continued Assignment statements come in very many forms throughout the program A single value can be assigned to another variable. letter X= 40 A variable can have the value of the a formula placed the. Y=3X +4 A variable can be a holding place for the answer of a function or module. Y = sqrt(x)
operators
Number types Integers have no decimal point Real numbers have a decimal part Character a single character String a group of characters between single quote or double quotes
Some Facts Order of operations Trig functions sine, cosine, tangent In the math library, which needs to be imported at the beginning of a program. Use the following statement: from math import *