Programming In Python
[Pages:37]Programming In Python
Python is a simple and clean language, which you will work with in CS 2370 Introduction to Programming.
Below is a favorite.py program.
#Python program for the #favorite number algorithm
#get the favorite number n=int(input ("What is your favorite number? "))
#compute the number n=n+1
#write the output print() print("My favorite number is more than that,", n)
#finish up input("\n\nPress the Enter key to exit");
Its execution goes like this with an IDLE:
=RESTART: favorite.py = What is your favorite number? 5 My favorite number is more than that, 6 Press the Enter key to exit
1
Play with Python
To work with Python, you need to download it first. Check out the How to install piece.
Once installed, you can use its IDLE app to "Run" this program:
= RESTART: favorite.py What is your favorite number? 5 My favorite number is more than that, 6 Press the Enter key to exit >>>
You can also run it with CMD if it is activated.
2
How to run it with cmd:
E:\testPython>dir
04/19/2020 03:18 PM
295 favorite.py
04/23/2020 07:57 AM
415 travel1.py
2 File(s)
710 bytes
2 Dir(s) 1,446,780,411,904 bytes free
E:\testPython>more favorite.py
#Python program for the
#favorite number algorithm
#get the favorite number n=int(input ("What is your favorite number? "))
#compute the number n=n+1
#write the output print() print("My favorite number is more than that,", n)
#finish up input("\n\nPress the Enter key to exit");
E:\testPython>favorite.py What is your favorite number? 5
My favorite number is more than that, 6
Press the Enter key to exit E:\testPython>
3
Let's start with data....
To free ourselves from having to manage data movement within memory, we will not use address, but names, of data.
Names in a programming language are called identifiers. In Python, an identifier can be any combination of letters, digits, and the underscore symbol ( ), as long as it does not begin with a digit.
The general rule is that, for a variable, whose value may change, we use the so-called camel case: First word begins with a lowercase letter, additional words begin with uppercase letters, e.g., finalTotal.
For constants, whose value never change, we use capital letters throughout, e.g., CONSTANT.
4
Typeless Python
Since a sequence of binary digits can be interpreted as a whole number, a negative number, an integer, etc., we almost always specify a type for any data. This helps us to understand its meaning, and also allows a computer to set aside a piece of space to hold its value.
But, in Python, a variable doesn't have a fixed data type. After an assignment statement
myNumber = 15
the binary string kept in memory location myNumber is interpreted as an integer.
The execution of the following assignment
myNumber = 65.81
will let Python believe it is a decimal value. And then the following assignment
myNumber = "This is my number"
will make it a string.
5
A list
Beside individual datum, we can create a whole collection, a list, of logically related variables at one time. It will be referred as array in other languages, such as C and Java.
roster = ["Martin", "Susan", "Chaika", "Ted"] print(roster) The output is
['Martin', 'Susan', 'Chaika', 'Ted']
The following statement
print(roster[1]) produces output of "Susan" as its index starts with 0.
6
Statements
Remember that any algorithm goes through three parts: input, processing, and output?
An input statement collects a specific value from the user for a variable within the program. For example, the following
n=int(input ("What is your favorite number? ")) What is your favorite number? 5
gets a value, converts it to an integer, then assign it to a variable n.
Once the processing is done, the following output statement displays that value on the screen, and any other information
print("My favorite number is more than that,", n) My favorite number is more than that, 6
7
Examples
We use several input/output statements in the following travel1.py program:
speed = input("Enter your speed in mph: ") speed = int(speed) distance = input("Enter your distance in miles: ") distance = float(distance)
time = distance/speed print("At", speed, "mph, it will take") print("%5.2f" % time, "hours to travel",\
distance, "miles.") input("\n\nPress the Enter key to exit")
Notice that we convert speed to an integer, and distance to a floating number, and the format string, "%5.2f", specifies at most five digits in total, and two digits after the decimal point of a floating number.
Enter your speed in mph: 72 Enter your distance in miles: 305 At 72 mph, it will take
4.24 hours to travel 305.0 miles.
8
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- macro programming in excel
- programming in excel
- programming in java pdf
- sort dictionary in python by values
- programming in matlab
- object oriented programming in matlab
- programming in basic
- database programming in java
- database programming in python
- run python script in python shell
- object oriented programming in python
- python programming in visual studio