Programming In Python

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

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download