Programming In Python

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 IDE:

=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.

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

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

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

Google Online Preview   Download