Presentation is loading. Please wait.

Presentation is loading. Please wait.

Text Copyright (c) 2017 by Dr. E. Horvath

Similar presentations


Presentation on theme: "Text Copyright (c) 2017 by Dr. E. Horvath"— Presentation transcript:

1 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

2 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

3 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

4 Precedence ( ) Parentheses ** Exponentiation +x, -x Positive, Negative
* / % Multiplication, Division, Remainder + - Addition, Subtraction <, <=, >, >=, !=, == Relational Operators not x Boolean NOT and Boolean AND or Boolean OR

5 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

6 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

7 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 = h1 = round(h,2) h1 = 45.44 h2 = round(h,1) h2 = 45.4 Text Copyright (c) 2017 by Dr. E. Horvath

8 Text Copyright (c) 2017 by Dr. E. Horvath
Formatting Output pi = 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

9 Text Copyright (c) 2017 by Dr. E. Horvath
Formatting Output pi = 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

10 Text Copyright (c) 2017 by Dr. E. Horvath
Formatting Output pi = 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

11 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


Download ppt "Text Copyright (c) 2017 by Dr. E. Horvath"

Similar presentations


Ads by Google