

Python also allows the if-else statement to be constructed in a variety of variant forms. Print('You do not meet the requirements to ride.') Height = int(input('Enter your height in inches: ')) We can rewrite the previous example by saying that you must be both older than 12 and taller than five feet to ride the roller coster:Īge = int(input('Enter your age in years: ')) A compound test is a test made up of two or more ordinary tests joined together by the logical connectors and or or. Print('You are too young to ride the roller coaster.')Ī useful alternative to putting one if-else statement inside another is being able to form a compound test. Print('You must be at least 5 ft tall to ride.') X = float(input('Enter a value for x: ')) Here are two examples of the if-else statement in use. = means 'set the thing on the left to the value on the right' and not 'compare the thing on the left with the thing on the right.' A very common mistake that beginning programmers will make is to us = in place of = in a test. Here is a table of the available comparison operators. The most common way to form tests for an if-else statement is by using comparison operators. Once the statements in the selected branch have run, execution continues with the next statement after the if-else. Otherwise, the statements in the block after else will get executed. If the outcome of the test is true, the set of statements in the first indented block gets executed. The if-else statement begins by performing a test whose outcome can be either true or false. The basic form of the if-else statement is this: The basic construct in Python for making decisions and acting on different cases is the if-else statement. If you want to use concatenation with other data types you will have to convert that data to a string by using the str() command.Ĭompliment = 'You look good for someone who is ' + str(age) + '.' Height = float(input('Enter your height in meters: '))Īn important operation that the string data type supports is concatenation, which glues strings together to form larger strings.Ĭoncatenation only works with two string operands. If you want to interpret the input as a floating point real number you use this form instead: For example, if you want to interpret the user's input as an integer quantity you would use this form: If you want to interpret the text the user entered as a numeric type, you need to use a type conversion command. The input() command always returns text as its result, even in cases where you prompt the user to enter a number. As soon as the user hits enter, their text will be stored in the variable that you assigned the input result to. When your program reaches an input command it will display the prompt and wait for the user to type some input. The text that appears in the input() is a prompt, which tells the user what information they are expected to enter. You can also obtain some text by using the input() command, which prompts the user to enter some text. You can assign a text literal to a variable, which looks like this: You can create text in one of two ways in a Python program. Python also features a string data type, which is used to store text. The integer remainder operator yields the remainder that is left over after we divide one int by another int.Ĭonverting numbers to text and text to numbers

The integer division operator returns the quotient that results when we divide one int into another. Python also includes a couple of special operators for use with int data, the integer division operator // and the integer remainder operator %. If one or more of the operands in any arithmetic operation are floats, the result will always be a float. Since the result of many divisions involving two integer operands can not be represented as an integer, Python defaults to converting the result of every division with two integer operands to the float data type. If you do arithmetic with variables with integer values the results of those computations will be integers, except in the case of division. If you store an integer value in a variable, Python will automatically use the int data type for that quantity. The int data type is used to store integers, while the float data type is used to store non-integral floating point numbers. Python actually uses two different data types to store numerical values. We have already seen a few examples of how Python can do numerical computations.
