cs2304: data types - virginia...
TRANSCRIPT
CS2304: Python for Java Programmers
Monti 2014
CS2304: Data Types
CS2304: Python for Java Programmers
Monti 2014
Side point: Comments
• Like other languages Python lets you put free text inside of source code.
• Comments are ignored by the Python interpreter and therefore do not execute.
• There are two types of comments you can use: • Whole line (or rest of line) comments start with a # and work like // in Java/C/C++.
• Python also has multi-line comments: • Three double quotes to start and stop the comment “”” “”” .
• We are basically creating a big unused string. • These strings/comments can also used as doc-strings.
CS2304: Python for Java Programmers
Monti 2014
Side point: Using The Interpreter
• You can follow along in the interactive interpreter (the part with >>>, just type ‘python3’.
• Using the the interpreter you can treat Python kind of like a calculator.
• It’s also convenient for quickly testing code. • Everything we look at today will work in standard
Python code outside of the interpreter.
CS2304: Python for Java Programmers
Monti 2014
Numeric Types and Operations
• Python has integer and double types (as well as a few others) like Java/C/C++.
• Variables can switch between integer and double types (or anything else) from line to line.
• Python 3 reacts more gracefully (?) when dealing with integer division.
• Python supports the standard mathematical operations you are familiar with: +, -, *, / and %.
• You can also calculate powers with **.
CS2304: Python for Java Programmers
Monti 2014
Identifiers and Variables
• These have a similar naming scheme to Java/C++. • Identifiers (variables, functions, etc.) must start
with a letter or an underscore. • The underscore has a particular meaning we’ll see later on.
• After the first character the rest can be: numbers, letters, and underscores.
• Python suggests that variables and function names be all lower case with underscores.
• There are different style rules for class names, modules: • Style guide: http://www.python.org/dev/peps/pep-0008/
CS2304: Python for Java Programmers
Monti 2014
Examples: Operators and Variables
• Using variables and operators should work more or less as you expect.
• However, variables don’t have to be declared in the same sense as Java/C++.
• You will want to give variables a value though:
>>> width = 20 >>> height = 5 * 9 >>> width * height 900
CS2304: Python for Java Programmers
Monti 2014
An Important Difference: Division
• By default (using /), division converts the operands and produces a floating point result.
• Unlike Java/C++ where you need to carefully consider the types. • Try 1 / 2 in Python vs. C++/Java, it should be 0.5 in Python.
• Python has an additional operator “//”, which will discard the fractional part. • So, 1 // 2 = 0 rather than 0.5.
• Note: this is different from Python 2.X, which behaved like Java/C/C++.
CS2304: Python for Java Programmers
Monti 2014
Strings in Python
• A collection of characters ex. “Hello World”. • Single and double quotes are pretty much
interchangeable: • So ‘Hello World’ is the same as “Hello World”. • While ‘’ or “” are acceptable, they do need to “match” so ‘ ”
won’t work. • Remember “”” “”” can be used for multi-line comments.
• Strings in Python are for the most part very flexible. • “+” Will concatenate two or more strings. • “*” Can be used as well, ex. “hello”*3.
CS2304: Python for Java Programmers
Monti 2014
String Basics
• Strings can use bracket notation like arrays/strings in other languages.
• Oddly, at least coming from another language, you can also use negative indices.
• You start at the end of the string and count backwards:
>>> s = “Hello World” >>> s[0] ‘H’ >>> s[3] ‘l’
>>> s[-1] ‘d’
CS2304: Python for Java Programmers
Monti 2014
String Slicing
• String “slicing” is supported, giving you substrings:
• Strings are immutable, so trying to modify an individual cell will cause an error:
• You can always create another string though.
>>> s = “Hello World” >>> s[0:3] ‘Hel’ >>> s[3:6] ‘lo ’
>>> s[0] = “L” # TypeError
CS2304: Python for Java Programmers
Monti 2014
Iterating Through Strings
• Recall: We can use a for loop to move through a string.
• The loop will iterate through the characters, printing them one by one:
H e l …
for x in s: print(x)
CS2304: Python for Java Programmers
Monti 2014
Basic Output
• Python provides the print() function for basic I/O:
• The print function is a little different in Python
2.X, it doesn’t need parentheses.
>>> print(s) Hello World >>> print(s[2:]) llo World >>> print(s[2:], 1, 0) llo World 1 0
CS2304: Python for Java Programmers
Monti 2014
Basic Input
• If you want to prompt for something, use the input function:
• Be careful: ‘7’ is a string that need to be
converted to be used as a number:
>>> j = input("Enter a number: ") Enter a number: 7 >>> print(j) 7
>>> print(int(j)+1) 8