DOC PREVIEW
TAMU CSCE 110 - Variables
Type Lecture Note
Pages 3

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSCE 110 1nd EditionLecture 2Outline of Last Lecture:I. Getting StartedA. BasicsB. Types of input/outputII: OperatorsA. Mathematical OperatorsB. Comparison OperatorsC. Conjunctive OperatorsOutline of Current Lecture:I. VariablesII. Raw InputA. What are they?B. Key functionsIII. More on StringsCurrent LectureI. VariablesJust as in math, variables in Python are used to represent other values. We can choose any combination of letters and numbers (but note that a variable cannot have a number as the first character or be the name of a key function) as a variable name, and they are case-sensitive. To assign a value or expression to a variable, we use the = character.For example:>>> apple = 12345>>> print apple>>> 12345Notice that we use "=" instead of "==". That is because "=" is used for assignment of variables, and "==" is used to mean that two objects are actually equal. II. Raw InputA. Up until now, we have only been working in the Python Shell. We can work outside of this in a new file, or project. Here is where we type a program that we later run, and the output will begiven in the Python Shell. We can have a program with a variable that the user sets a value for when asked by the computer. To do this, we can use "raw_input." This will give the user the opportunity to come up with new values for the variable each time the program is run. Here's an example:These notes represent a detailed interpretation of the professor’s lecture. GradeBuddy is best used as a supplement to your own notes, not as a substitute.>>> name = raw_input("Please enter your name: ")>>> print "Hello", name, "- good to see you!"The program will pause and the phrase "Please enter your name: " will appear in the Python Shell, and the user will manually enter a value. The program will then continue running. We separate different types of values (strings from integers, etc.) by using a comma. This automatically adds a space between the two values when they are printed. Another way to add spaces without using the comma is this way:>>> print "Hello " + name + " - good to see you!"That time we just added a space within the strings themselves.Here's another example of a program that uses "raw_input":>>> name = raw_input('Please type your first name: ')>>> name2 = raw_input('Please type your middle name: ')>>> name3 = raw_input('Please type your last name: ')>>> print name, name2, name3Another way we could write the last line is:>>>print name + " " + name2 + " " + name3This time we added a space in the form of a separate string to set apart the variables. Here's an example where we use "raw_input" to ask the user for a number.>>> number = raw_input('Please enter a number: ')>>> new = int(number) + 1>>> print newThe number that the user inputs when asked, though a number, is in the form of a string. In order to then use the variable "number" as an integer in a mathematical computation, we have to convert it from a string to an integer using int(variable). B. Here are the key functions that we cannot use as variable names. Notice that none of them begin with capital letters.and as assert breakclass continue def delelif else except execfinally for from globalif import in islambda not or passprint raise return trywhile with yieldIf we capitalize the first letter, these words will no longer be functions. That means we could usethese words as variable names if we capitalized the first letter, but to avoid confusion, one should be discouraged from doing so. III: Diving further into stringsJust as we are able to use mathematical operators on integers and floats, we can alter strings using the same symbols. Here are some examples:>>> x = '56' + 'string'>>> print x>>> 56stringNotice that we can have numbers as strings if we place them in quotations. >>> x = 'yes' * 4>>> print x>>> yesyesyesyesUsing the multiplication operator, we can repeat an entire string however many times we desire.>>> 45 + True>>> 46Normally if we added a string to an integer or float, we would get an error back. This is not whathappens when we add a number to a Boolean. With Booleans, a True is equivalent to 1, and False is equivalent to


View Full Document

TAMU CSCE 110 - Variables

Type: Lecture Note
Pages: 3
Documents in this Course
06-IO

06-IO

29 pages

21-OOP

21-OOP

8 pages

key

key

6 pages

21-OOP

21-OOP

8 pages

Load more
Download Variables
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Variables and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Variables 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?