Coder Social home page Coder Social logo

cssi-4.5-python-conditional-statements-al's Introduction

#Python Conditional Statements

#Objectives:

  • Understand the syntax of conditional statements
  • Use conditional statements
  • Understand what a boolean is

#Motivation Sometimes we only want our code to run if certain conditions are in place. If a condition is met, then python should run the code otherwise something else should happen.

#Conditionals Conditional statements are a type of control flow: They can control which parts of code get executed, and which do not. The basic conditional statement is the if statement:

if condition_1:
    run this block of code

Here is an example with two variables:

yourAnimal = input('What is your favorite animal? ')
myAnimal = "dog"

if yourAnimal == myAnimal:
  print "That's my favorite animal, too!"

#Comparison Operator

  • "==" means "are these two things equal?".
  • "=", means assignment - you're setting a variable equal to a value.

#Boolean Values False and True are special values called booleans. Booleans can be assigned to variables. For example:

x = (3 == 5)
print x

First, we calculate (3 == 5). This is a question: "Is 3 equal to 5?". The answer to that question is False: 3 is not equal to 5.

So, when we say x = (3==5), this is equivalent to x = False. We're setting X to the boolean value False. Now, when we print x, we just print out False.

Here are some important boolean operators:

#Else and Elif conditional statements There are a few other conditional statements that work together with if. For example, you often want to do something if the condition failed:

yourAnimal = input('What is your favorite animal? ')
myAnimal = "dog"
if yourAnimal == myAnimal:
  print "That's my favorite animal, too!"
else:
  print "I don't think you understand how cool dogs are."

An "else" clause should always follow an "if" clause, because "else" means "otherwise." You wouldn't ever start a conversation with "Otherwise, do this!", so it doesn't make sense to start with an else, when there's not an if right before it.

There's also "elif". It's an "else" and "if" glued together - if the first thing works, do that; otherwise, try the second condition; else...

if x > 500:
  print "x is really big"
elif x > 50:
  print "x is sort of big"
elif x < 0:
  print "x is negative!"
else:
  print "x is not very interesting"

#Student Mini Challenge In your practice.py try writing a conditional statement with an if condition, elif condition and else condition

  • Pick a Number: Write a program that makes the user guess a number 1-10. If their guess is correct, tell them they've won. If their guess is too high or too low, give the user a hint. you will need to use python's input() method.

  • userGuess = input('Guess a number between 1-10')

  • Stretch: Phone Number Validator: Write a program that checks if a string is formatted correctly as a phone number (312)867-5309.

  • Check if there are 10 integers

  • Check if the first three integers are between two parenthesis

  • Check to make sure that a dash separates the 6th and 7th integer.

View Python Conditional Statements on Learn.co and start learning to code for free.

cssi-4.5-python-conditional-statements-al's People

Contributors

georgiadavis avatar nanselmo avatar sarogers avatar fislabstest avatar

Watchers

James Cloos avatar  avatar Mohawk Greene avatar Victoria Thevenot avatar Bernard Mordan avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar  avatar  avatar  avatar Ben Oren avatar Matt avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Dominique De León 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.