CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output Dr. Tiffani L. Williams Department of Computer Science and Engineering Texas A&M University Spring 2011
Python Python was developed in 1989 by Guido van Rossum in the Netherlands. Python was released for public distribution in early 1991. How did Python begin? van Rossum was having a hard time getting the job done with the existing tools available. He envisioned that there was an easier way to get things done. While Python has been around for over 15 years, Python is still relatively new to general software development. However, it has a lot of a lot of support from the community and new users are finding themselves programming in Python (and loving it) everyday.
Be an Explorer Since you are learning a new language, it is very important that you experiment with the material. Be curious about trying different things. Don t be afraid to produce an error. That s how we learn best.
Interactive Execution
Hello, World! Python s print statement is the tool for displaying program output to your users. Type the following in IDLE > > > print Hello, World!
Using Python as a Calculator > > > 3 + 4 > > > 15 / 3 > > > 12 * 10 + 4
Operators Mathematical operators: +, -, *, /, %, ** addition (+) subtraction (-) multiplication (*) division (/) modulus or remainder (%) exponenentiation (**) Comparision operators: <, <=, >, >=, ==,!= strictly less than (<) less than or equal to (<=) strictly greater than (>) greater than or equal to (>=) equal to (==) not equal to (!=) Expression conjunctive operators: and, or, not
Variables and Assignment > > > counter = 0 > > > miles = 1000.0 > > > name = Bob > > > counter = counter + 1 > > > kilometers = 1.609 * miles > > > print miles 1000.0 > > > print counter 1 > > > print kilometers 1609.0
Variables Variables are the set of valid strings that are allowed as names in a computer language such as Python. The rules for forming Python variables are: First character must be a letter or underscore (_) Any additional characters can be alphanumeric or underscore Case-sensitive No variables can begin with a number. No symbols other than alphanumerics or underscores are ever allowed. No variable can be the same as keywords, which form the foundation of the language.
Keywords and as assert break class continue def del elif else except exec finally for from global if import in is lambda not or pass print raise return try while with yield
Numbers Python supports five basic numerical types. int (signed integers) long (long integers) bool (Boolean values) float (floating point real numbers) complex (complex numbers) Here are some examples. int: 100 200-437 long: -84140l 299556678883L bool: True False float: 3.456-33.55 34.1 complex: 6.23+1.5j -1.23-875J 0+1j
Strings Strings are a contiguous set of characters in between quotation marks. Strings are immutable sequences. A program can refer to elements or subsequences of strings. However, strings cannot be modified in place. Python allows for either pairs of single or double quotes. Triple quotes (three consecutive single or double quotes) can be used to escape special characters. Subsets of strings can be taken using the index ( [ ] ) and slice ( [ : ] ) operators, which work with indexes starting at 0 in the beginning of the string and working their way from -1 at the end. Slice ( [] ) gives the character from the given index or location Range slice ( [x:y] ) gives the characters starting at index x and ending at index y -1 The plus ( + ) sign is the string concatenation operator. The asterisk ( * ) is the repetition operator.
String Examples > > > string = "Texas A&M University" > > > string[0] T > > > string[2:5] xas > > > string[4:] s A&M University > > > string[:6] Texas > > > string * 2 Texas A&M UniversityTexas A&M University > > > string = It\ s a girl! > > > string "It s a girl!"
Writing Python Programs
User Input 1. Type the following text directly into IDLE. > > > name = raw_input("please enter your name: ") > > > print "Hello", name, "- good to see you!" 2. Type the above text into a file called hello.py and save it as hello.py. Afterward, run the program in IDLE. 3. Discuss the difference between the two different approaches for entering Python programs.
Write the Following Python Programs 1. Full name greeting. Write a program that asks for a person s first name, then middle, and then last. Afterwards, it should greet the person using their full name. 2. Guessing game? Write a program that asks a person to guess a number. Add 1 to the guessed number, and then suggest that their guess was close to being correct.