Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Programming In Python, Study notes of System Programming

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

Typology: Study notes

2021/2022

Uploaded on 09/12/2022

shafi
shafi 🇺🇸

3.9

(9)

221 documents

1 / 37

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Partial preview of the text

Download Programming In Python and more Study notes System Programming in PDF only on Docsity!

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+

#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

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.

Let’s start with data....

To free ourselves from having to manage data movement within memory, we will not use ad- dress, 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 under- score 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.

Typeless Python

Since a sequence of binary digits can be inter- preted 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.

will let Python believe it is a decimal value. And then the following assignment myNumber = "This is my number"

will make it a string.

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 out- put 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

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.

An example

The following format.py program

numerator = 7 denominator = 2 quotient = numerator/denominator intQuotient = numerator//denominator remainder = numerator % denominator print("The result of", numerator,
"/", denominator, "is", quotient) print("The integer quotient of", numerator,
"/", denominator, "is", intQuotient) print("and the remainder is", remainder) print()

will produce the output

The result of 7 / 2 is 3. The integer quotient of 7 / 2 is 3 and the remainder is 1

Notice that 7 / 2=3.5, 7 // 2=3, and 7 % 2 =

Assignment: Play with all the above programs.

Learn with HackerRank

  1. Go to https://www.hackerrank.com/, sign up and then log in
  2. Select Python

Control statements

There are three control structures: sequential, conditional and loop in any program.

We have seen so far several sequential struc- tures, which brings us from the beginning to the end.

The other two change the flow based on a con- dition, which we saw quite a bit in the previous chapters.

As we said, a program is just a combination of

these three control structures. ,

What conditions?

A condition could be something simple: Operator Symbol Example Result Same == 2==5 false Less than < 2<5 true Less than or equal to <= 5<=5 true Greater than > 2>5 false Greater than or equal to >= 2>=5 false Not the same != 2!=5 true

It can also be a complex one by combining the simpler ones with boolean operators.

Operator Symbol Example Result AND and (2<5) and (2>7) false OR or (2<5) or (2>7) true NOT not not (2==5) true

Each of the three logic operators is associated with a logic gate that we discussed earlier in Chapter 5 on computer hardware.

Both the conditional and loop structures will decide what to do next, based on evaluation of such a condition.

A simple variant

A one branch conditional statement

if Condition: S

looks like the following:

Below is an example:

if B < (A + C): A = 2*A

Another example

More stuff can be put into a block, e.g., the following pbCondition.py program snack = "pb & j" if snack == "pb & j": print("yummy") print("sticky") else: print("Must be pizza") print("That’s All, Folks") produces yummy sticky

While the following pbCondition2.py program snack = "pb & j" if snack != "pb & j": print("yummy") print("sticky") else: print("Must be pizza") print("That’s All, Folks") produces Must be pizza print("That’s All, Folks")

An elif example

We use the keyword elif as an abbreviation of else if. The following elif.py program provides an example of its usage.

Imagine you have 6 subjects and Grand total is 600

Totalmarks = int(input(" Please Enter Your Total Marks: "))

if Totalmarks >= 540: print(" Congratulations! ") print(" You are eligible for Full Scholarship ") elif Totalmarks >= 480: print(" Congratulations! ") print(" You are eligible for 50 Percent Scholarship ") elif Totalmarks >= 400: print(" Congratulations! ") print(" You are eligible for 10 Percent Scholarship ") else: print(" You are Not eligible for Scholarship ") print(" We are really Sorry for You ")

The following is a working session of running this program.

= RESTART: elif.py Please Enter Your Total Marks: 564 Congratulations! You are eligible for Full Scholarship

Labwork 2

0.Work with “Loops”, “Write a function”, and “Print Functions”. Again, start with the Prob- lem, look at the Tutorial for help, then practice and submit your code.

Once you are comfortable with these basic skill, complete the following assignments.

  1. Write a program that will print 1 if two numbers are the same; and 0 if not.
  2. Write a program that will prompt for an operator, and then print 1 if it is the same of ‘+’, else print 0.
  3. Write a program that asks for two numbers, and an operator. This program will print out the sum of these two numbers if the operator is ‘+’, their difference if the operator is ‘-’, and their product if the operator is ‘*’.