





























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 37
This page cannot be seen from the preview
Don't miss anything!
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
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.
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.
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.
Remember that any algorithm goes through
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
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.
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
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
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 one branch conditional statement
if Condition: S
looks like the following:
Below is an example:
if B < (A + C): A = 2*A
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")
We use the keyword elif as an abbreviation of else if. The following elif.py program provides an example of its usage.
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.