Text Copyright (c) 2017 by Dr. E. Horvath Creating Python 3 Scripts, Declaring Variables, Arithmetic Operators, and Formatting Text Copyright (c) 2017 by Dr. E. Horvath
Creating Python 3 Scripts Menu → Programming → Python 3 (IDLE) IDLE = Integrated Developers Environment File → New File Run → Run Module or press F5 to run Text Copyright (c) 2017 by Dr. E. Horvath
Text Copyright (c) 2017 by Dr. E. Horvath Declaring Variables An assignment statement is sufficient to declare a variable. The type of the data is based on the values assigned to a variable. count = 5 temperature = 98.6 name = “Your name” print(“count “ + str(count)) print outputs a value in the shell Text Copyright (c) 2017 by Dr. E. Horvath
Precedence ( ) Parentheses ** Exponentiation +x, -x Positive, Negative * / % Multiplication, Division, Remainder + - Addition, Subtraction <, <=, >, >=, !=, == Relational Operators not x Boolean NOT and Boolean AND or Boolean OR
Text Copyright (c) 2017 by Dr. E. Horvath Input data count = input(“Enter the count”) Prompt the user for input count = int(count) Convert to an integer print(“count “ + str(count)) print outputs a value in the shell + concatenate strings str(count) covert to a string for output temperature= input(“Enter the temperature”) temperature = float(temperature) Convert to a floating point value name = input(“Enter your name”) print(“name = “ + name) Text Copyright (c) 2017 by Dr. E. Horvath
Text Copyright (c) 2017 by Dr. E. Horvath Escape Characters print(“\”Your name \”\n”) \” Print a double quote \n New line character Text Copyright (c) 2017 by Dr. E. Horvath
Text Copyright (c) 2017 by Dr. E. Horvath round function The round function rounds a floating point value to a specific number of decimal places. h = 45.442564 h1 = round(h,2) h1 = 45.44 h2 = round(h,1) h2 = 45.4 Text Copyright (c) 2017 by Dr. E. Horvath
Text Copyright (c) 2017 by Dr. E. Horvath Formatting Output pi = 3.14159 name = “Your name” msg = "pi: "+ format(pi,'7.2f') 7 spaces, 2 decimal places of precision f indicates floating point msg = "pi: "+ format(pi,'0.3e') no additional spaces before the decimal point 3 decimal places of precision e indicates exponential Text Copyright (c) 2017 by Dr. E. Horvath
Text Copyright (c) 2017 by Dr. E. Horvath Formatting Output pi = 3.14159 name = “Your name” msg = "pi: "+ format(p,'10,.2f') 10 spaces, comma for thousand places, 2 decimal places of precisions f indicates floating point msg = "name: "+ format(name,'15s'') 15 spaces s indicates string data Text Copyright (c) 2017 by Dr. E. Horvath
Text Copyright (c) 2017 by Dr. E. Horvath Formatting Output pi = 3.14159 hours = 24 msg = “pi {:7.2f} hours {:3d}".format(pi,hours) 7 spaces, 2 decimal places of precision f indicates floating point 3 spaces for an integer data Text Copyright (c) 2017 by Dr. E. Horvath
Text Copyright (c) 2017 by Dr. E. Horvath Formatting Output msg = "pi {0:7.2e} hours {1:04d}".format(pi,hours) 7 spaces before the decimal point 2 decimal places of precision e indicates exponential preceeding zero, 4 spaces for an integer name = “Your name” msg = Your name is {0:>15s}".format(name) 15 spaces > indicates right align s indicates string Text Copyright (c) 2017 by Dr. E. Horvath