10/23/07. >>> Overview * if else * returns * input.

Slides:



Advertisements
Similar presentations
Variables and References in Python. "TAGAGAATTCTA” Objects s Names References >>> s = “TAGAGAATTCTA” >>>
Advertisements

2/7/2008. >>> Overview * boolean * while * random * tuples.
Python Programming Language
Introduction to Computing Using Python Methods – on strings and other things  Strings, revisited  Objects and their methods  Indexing and slicing 
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
Conditional Statements Introduction to Computing Science and Programming I.
Week 5 while loops; logic; random numbers; tuples Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Structured programming
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Week 2 ______ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
Week 7 Lists Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is licensed.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
Unit 5 while loops; logic; random numbers; tuples Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
January 24, 2006And Now For Something Completely Different 1 “And Now For Something Completely Different” A Quick Tutorial of the Python Programming Language.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
11/27/07. >>> Overview * objects * class * self * in-object methods * nice printing * privacy * property * static vs. dynamic * inheritance.
Week 4. >>> Overview * if else * returns * input.
Python for Informatics: Exploring Information
COMPUTATION WITH STRINGS 2 DAY 2 - 8/29/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
If statements while loop for loop
Week 7 Review and file processing. >>> Methods Hello.java public class Hello { public static void main(String[] args){ hello(); } public static void hello(){
Python Conditionals chapter 5
COMPUTATION WITH STRINGS 1 DAY 2 - 8/27/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
Input and Output CMSC 120: Visualizing Information Lecture 4/10.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Python – May 16 Recap lab Simple string tokenizing Random numbers Tomorrow: –multidimensional array (list of list) –Exceptions.
1/10/2008. >>> About Us Paul Beck * Third quarter TA * Computer Engineering * Ryan Tucker * Second quarter TA * Computer.
10/9/07 ______ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
Exception Handling and String Manipulation. Exceptions An exception is an error that causes a program to halt while it’s running In other words, it something.
8/2/07. >>> About Me Scott Shawcroft * Junior * Computer Engineering * Third Quarter TA * Creative Commons Intern * Small-time Open Source Developer
Python Files and Lists. Files  Chapter 9 actually introduces you to opening up files for reading  Chapter 14 has more on file I/O  Python can read.
LECTURE 5 Strings. STRINGS We’ve already introduced the string data type a few lectures ago. Strings are subtypes of the sequence data type. Strings are.
Python. Lecture 02. Найдите все составные числа меньшие N, которые представимы в виде произведения двух простых чисел.
10/30/07. >>> Overview * boolean * randomness * while * tuples.
11/13/07. >>> Overview * lists/arrays * reading files * writing files.
10/30/07.
10/16/07.
The need for Programming Languages
Catapult Python Programming Wednesday Morning session
10/9/07 ______ < Moo? > \ ^__^ \ (oo)\_______ (__)\ )\/\
CSc 120 Introduction to Computer Programing II Adapted from slides by
Sequences and Indexing
Introduction to Python
Python: Control Structures
ECS10 10/10
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.
CompSci 230 Software Construction
Iterations Programming Condition Controlled Loops (WHILE Loop)
Methods – on strings and other things
Week 4 gur zntvp jbeqf ner fdhrnzvfu bffvsentr
1/22/2008.
CS190/295 Programming in Python for Life Sciences: Lecture 6
Python - Strings.
Topic 1: Problem Solving
while loops; logic; random numbers; tuples
Chapter 4: Control Structures I (Selection)
Python Programming Language
Methods – on strings and other things
15-110: Principles of Computing
What is x? x = 12 %
Introduction to Computer Science
Control Flow statements
Presentation transcript:

10/23/07

>>> Overview * if else * returns * input

>>> if Like many things in the transition from Java to Python, curly braces are replaced with colons and whitespace, the parentheses are dropped and &&, || and ! change. // 1 for english // 2 for german int translator = 1; if (translator == 1) { english(); } else if (translator == 2) { german(); } else { System.out.println("None"); } Translator.java translator = 1 if translator==1: english() elif translator==2: german() else: print "None" translator.py < > <= >= == != or and not < > <= >= == != || && ! Java python Notice: "else if" becomes "elif"

>>> strings Just like in Java, strings are objects. Here are some things you can do with them: s.capitalize() "wow".capitalize() => "Wow" s.endswith( ) "wow".endswith("w") => True s.find( ) "wow".find("o") => 1 s.islower() "wow".islower() => True s.isupper() "wOw".isupper() => False s.lower() "wOw".lower() => "wow" s.split( ) "hmm wow".split(" ") => ["hmm","wow"] s.startswith( ) "hmm".startswith("hm") => True s.strip() " ack ".strip() => "ack" s.swapcase() "wOw".swapcase() => "WoW" s.upper() "wow".upper() => "WOW" string methods

>>> strings as sequences As we saw with loops, python has certain sequence types such as lists. Strings are also sequences. Indexes start with 0 the left and -1 on the right. seq[ ] "look!"[3] => "k" "look!"[-1] => "!" seq[ : ] "shocking"[4:] => "king" "shocking"[3:5] => "ock" "shocking"[-3:] => "ing" len( ) len("whoa") => 4 sequence operations "shocking" from the front from the back Indexing example: "shocking"[2:-4] => "oc"

>>> return Returns in python are super easy. Simply "return " instead of "return ;" and forget about the types. def funky(s): if len(s)<=3: return s.lower() else: return s.upper() s1 = funky("wow") print s1 #"wow" s2 = funky("whoa") print s2 #"WHOA" funky.py

>>> input() vs. raw_input() There are two ways of getting input. The first is input(). It takes in input until enter is hit and then tries to interpret it into python. However, this way only works well for numbers. The second way is to use raw_input() which returns the entire line as a string. Once this is done, the string can be split into smaller strings and changed to the desired type. >>> x = input("yes? ") yes? y Traceback (most recent call last): File " ", line 1, in NameError: name 'y' is not defined >>> x = input("yes? ") yes? 2 >>> print x 2 >>> x = input("num? ") num? 2.0 >>> print x 2.0 #take a number in x = input("x? ") print x #take a sentence to tokenize it sent = raw_input("sentence: ") for w in sent.split(" "): print "word: " + w inputs.py

>>> igpay atinlay Translators: 1. Angry. 2. Pig Latin. Which translator would you like to use? 2 What would you like me to translate? Look! Pig latin! Translated: Ooklay! Igpay atinlay! yossarian ~ $ python translate.py

Except where otherwise noted, this work is licensed under © 2007 Scott Shawcroft, Some Rights Reserved Python® and the Python logo are either a registered trademark or trademark of the Python Software Foundation. Java™ is a trademark or registered trademark of Sun Microsystems, Inc. in the United States and other countries.