Giter Site home page Giter Site logo

dsc-python-operators-functions-and-methods-lab-houston-ds-042219's Introduction

Built-in Python Operators, Functions, and Methods - Lab

Introduction

We've looked at some of the built-in methods, functions, and the operators in Python. These are all very powerful tools we can (and will) use in our code. Below, we'll put these new tools to use to solve the tests in this lab.

Objectives

In this lab you will:

  • Use built-in Python functions and methods
  • Use comparison operators to compare objects
  • Use logical operators to incorporate multiple conditions
  • Use identity operators to confirm the identity of an object

Instructions

Let's start by using some built-in functions and methods. Employ the appropriate functions and methods to get the intended result.

# Desired output: "HELLO, THERE"
yell_hello = "hello, there" 
yell_hello
# Desired output: "psst, hey"
whisper_hey = "PSST, HEY" 
whisper_hey
# Desired output: "Learn. Love. Code"
flatiron_mantra = "LEARN. LOVE. CODE." 
flatiron_mantra
# Desired output: str
type_string = "i'm a string" 
type_string
# Desired output: list
type_list = ["i'm", "a", "list"] 
type_list
# Desired output: 3
length_of_list = ["i'm", "a", "list"] 
length_of_list
# Desired output: "list"
longest_word_in_list = ["i'm", "a", "list"]
longest_word_in_list
# Desired output: 1
smallest_number = [1, 3, 4, 78]
smallest_number
# Desired output: 11
sum_of_numbers = [1, 2, 3, 5]
sum_of_numbers

Uncomment the code in each cell as you start working on them. For example, when you begin working on the first cell, remove # at the start of each line.

Note: The cmd+? keyboard shortcut comments or uncomments a given line of code!

Replace [COMPARISON], with the correct comparison operator to get the desired output, which you will find as a comment at the end of each line. See the example below.

# boolean_compare = False [COMPARISON] True # True 
=> boolean_compare = False != True # True
OR
=> boolean_compare = False != True

Once uncommented, you can check the output to see if your comparisons match the answers provided in the ending comments.

Remember the comparison operators are: ==, !=, <, >, <=, >=

# boolean_compare = True [COMPARISON] True # False
# boolean_compare2 = False [COMPARISON] True # False
# print(boolean_compare, boolean_compare2)
# number_compare = 10 [COMPARISON] 10 # True
# number_compare2 = -20 [COMPARISON] 30 # True
# number_compare3 = 4 [COMPARISON] 5 # False
# print(number_compare, number_compare2, number_compare3)
# string_compare = "stacy" [COMPARISON] "STACY" # True
# string_compare2 = "hey i love python!" [COMPARISON] "hi love python" # False
# string_compare3 = "this string is bigger than the other" [COMPARISON] "that is true" # True
# print(string_compare, string_compare2, string_compare3)

In the next section, do not use either == or != operators

# list_compare = [0, 0, 0, 0] [COMPARISON] [0, 0, 0] # True
# list_compare2 = [1, 0, 0] [COMPARISON] [0, 0, 0] # True
# list_compare3 = [0, 0, 0] [COMPARISON] [0, 0, 3] # False
# list_compare4 = [0, 0, 3, 0] [COMPARISON] [0, 0, 3] # True
# list_compare5 = [0, 0, 4, 0] [COMPARISON] [0, 0, 3] # False
# print(list_compare, list_compare2, list_compare3, list_compare4, list_compare5)

Practicing Identity and Logical Operators

In this next section, use the identity and logical operators to get the desired output as you did in the examples above using the comparison operators.

Remember the logical operators are and, or, & not; and the identity operators are is & is not

Use logical operators for this section

# logical_compare = 2 [LOGICAL] [] # []
# logical_compare2 = [LOGICAL] [] # True
# logical_compare3 = 0 [LOGICAL] [] # 0
# logical_compare4 = True [LOGICAL] 2 # 2
# logical_compare5 = 2 [LOGICAL] 3 # 2
# logical_compare6 = [LOGICAL] True # False
# logical_compare7 = False [LOGICAL] 2 # False
# print(logical_compare, logical_compare2, logical_compare3, logical_compare4,
#       logical_compare5, logical_compare6, logical_compare7)

Use identity operators for this section

# a = []
# b = a
# identity_compare = {} [IDENTITY] {} # False
# identity_compare2 = a [IDENTITY] b # True
# identity_compare3 = b [IDENTITY] [] # True
# identity_compare4 = 9 [IDENTITY] 10 # True
# identity_compare5 = "Same" [IDENTITY] "Same" # False
# identity_compare6 = [1,3,4] [IDENTITY] [1,2,3] # False
# print(identity_compare, identity_compare2, identity_compare3,
#       identity_compare4, identity_compare5, identity_compare6)

Summary

Great work! After all that, there's nothing we can't compare. Well, I guess apples and oranges might still be off the table. We practiced using comparison, logical, and identity operators in Python to compare elements of the same and different datatypes and/or values. Going forward, there will be plenty of instances where we will need to compare elements. So, it is important to have a good understanding of how each of these operators works. Don't worry, as with all concepts in programming, the more we work with something the better we understand it.

dsc-python-operators-functions-and-methods-lab-houston-ds-042219's People

Contributors

loredirick avatar mas16 avatar mike-kane avatar lmcm18 avatar mathymitchell avatar sumedh10 avatar fpolchow avatar

Watchers

James Cloos avatar Kevin McAlear avatar  avatar Victoria Thevenot avatar Belinda Black avatar Bernard Mordan avatar  avatar Joe Cardarelli avatar Sara Tibbetts avatar The Learn Team avatar Sophie DeBenedetto avatar  avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Scott Ungchusri avatar Nicole Kroese  avatar  avatar Lisa Jiang avatar Vicki Aubin avatar Maxwell Benton avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.