Programming In Python - Plymouth State University
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.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- ppyytthhoonn mmoocckk tteesstt iiiiii tutorialspoint
- programs list for class xii cs
- recursion programs com
- exploring data using python 3 charles r severance etsu
- tries and string matching stanford university
- using python for computing on elliptic curves very preliminary draft
- programs on string
- python if in range if not in range examples
- real python python 3 cheat sheet
- s python cheat sheet data science free
Related searches
- macro programming in excel
- programming in excel
- programming in java pdf
- programming in matlab
- object oriented programming in matlab
- programming in basic
- columbus state university in georgia
- columbus state university in ga
- shops in downtown plymouth ma
- database programming in java
- database programming in python
- object oriented programming in python