ITEC 4961/CSCI 4961 Web Science (Fall 2010)/Python

From Web Science 2009 class

Jump to: navigation, search

Class Docs

Class Exercise

  • What are three string operations? Explain their syntax and give sample output.
  • What is different between if-statements in C++ and in Python?
  • Define a function that takes two strings and an integer n as parameters. Within that function, create a new string such that the two strings are concatenated, then repeated n times with spaces in between. Return this string and print it.
  • Define a list with values 1 through 10. Perform the following operations:
    • Add 10 to each even value in the list
    • Insert values {7, 3, 8, 7, 2} to the front of the list.
    • Determine how many times the number 7 appears in the list.
    • Sort the list.
  • Define a class Dog with the following attributes: name, breed, age (in human years), cats chased. Create several instances of this class and store in a list. Perform the following operations:
    • Print the names of all dogs younger than 5
    • Print the ages of all dogs in dog years
    • The shelter gives dogs treats based on the number of cats they chased.
      • Fewer than 3 cats chased: One treat
      • Between 3 and 5 cats chased: Half a treat
      • More than 5 cats chased: No treat
    • Print a list of the dogs with their treat allowance.
  • Using the twitter API, post a tweet and list all the friends jahendler has. Do not use the python twitter library, use urllib2.


Links

Personal tools