Programming In Python
[Pages:36]Programming In Python
Python is a simple and clean language, which you will work with a lot in CS 2370 Introduction to Programming.
Below is a program, favorite.py, in Python.
#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 open it in the Python folder in the lab. If you want to download it in your own computer, check out the How to install Python piece on the course page.
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
Let's start with data....
To free ourselves from having to manage data movement within memory, we will not use physical address of data, but their mnemonic name.
A name of a variable in a programming language is called its identifier. 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.
3
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.
4
A list
Beside individual pieces of data, 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.
5
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
>>> n = n+1 >>> print("My favorite number is more than that,", n) My favorite number is more than that, 6
6
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.
7
Some other stuff
1. Two useful escape sequences are `\n', which is to insert a new line; and `\t' is to insert a tab character.
>>> print("This is my favorite \n\t yellow puppy dog.")
gets us
This is my favorite yellow puppy dog.
2. The print function prints a blank line if no information is passed to it.
The following printHelloWorld.py
print("Hello") print( ) print("World")
would print out
Hello
World
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 download
- fortran 90 formatted input output
- programming in python
- introduction to python programming course notes
- a guide to f string formatting in python
- mixed integer linear programming with python
- python programming 1 variables loops and input output
- 1 functions in python
- python ii introduction to data analysis with python
- ppyytthhoonn ssttrriinnggss tutorialspoint
- python websitesetup
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