[go: up one dir, main page]

0% found this document useful (0 votes)
29 views8 pages

Python Text Lesson by Brocode Yt 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views8 pages

Python Text Lesson by Brocode Yt 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

(Transcribed by TurboScribe.ai. Go Unlimited to remove this message.

The user is offline. So, the user is offline. I'll change the boolean to true.

The user is online. So with if statements, you can either write a condition, or you
could use a boolean. Alright everybody, so those are if statements.

Do some code only if some condition is true. Else, you can do something else. It's
a basic form of decision making.

And those are if statements in Python. Hey everybody, this is a remake of my Python
calculator program for absolute beginners. All you need to know to complete this
exercise is just if statements and how they work.

So let's get started. For this exercise, a user is going to select an arithmetic
operator. Operator equals input.

We will ask the user to enter an operator. This will be a plus for addition, minus
for subtraction, asterisk for multiplication, and a forward slash for division. You
could enter more than this, but I don't want to make this exercise too complicated.

We will create a variable of num1 to contain our first number. Let's say we would
like to add two numbers together. What is the first number going to be? Enter the
first number.

And let's do this with the second number. num2, enter the second number. Let me
show you something.

I'm going to add num1 and num2 together. num1 plus num2. We'll do a test run.

Enter an operator. I would like to use addition. Enter the first number.

10 and 11. Well, the result is 1011. When we accept user input, they are string
data types.

What we've ended up doing is string concatenation. We've concatenated the string of
11 to 10. That's why we ended up with 1011.

We'll have to convert these two strings to be floating point numbers by typecasting
them as a float. So enclose your input functions with a typecast of float. And now
we should be able to add those two numbers together.

So let's add 10 and 11. And we get 21.0. Depending on the operator that the user
selects, we'll use some if statements to determine that. We will check if our
operator variable is equal to a character of plus.

And for now, I'll write pass as a placeholder. We'll get back to this later. Else
if our operator is equal to minus, we will use subtraction.

And for now, I'll write pass. Else if operator is equal to an asterisk for
multiplication, we will multiply. Else if our operator is equal to a forward slash
for division, we will divide.

If our operator is addition, let's create a variable of result. Result equals num1
plus num2. For subtraction, it's going to be num1 minus num2.

Multiplication would be num1 times num2. Then division would be num1 divided by
num2. Then we just have to print the result.
Print our result. Be sure to do this with each of the else if statements as well.
And let's see what we have.

Let's add 5.5 plus 6.9. That gives us 12.4. Let's subtract 420 minus 0.69. That
gives us 419.31. Let's test multiplication. Multiply 3.14 times 3.14, which gives
us 9.8596. Then division. Let's divide 69 by 13.

And that gives us a really long number. So you could round a number if you would
like. We would enclose our result within the round function.

And we'll just update each of these print statements. This will round to the
nearest whole integer. So let's divide 420 by 13.

Let's say that we would like 3 digits after the decimal. Within the round function,
we could add comma 3 for 3 decimal places. Enter an operator.

Let's use division. Divide 420 by 69. So that gives me 6.087. So we can round to a
given digit after a decimal.

In this case, 3 places. What if somebody types in an operator that doesn't exist,
like the word pizza? Then I will divide two numbers. Well, let's add an else
statement.

If somebody selects some input that is invalid, let's let them know. I'll use an if
string. Let's say that the operator that the user has selected is not valid.

And let's try this again. Enter an operator. Pizza.

Enter the first number, 420 and 69. Pizza is not valid. Let's say is not a valid
operator instead.

That makes more sense. Pizza will be my operator. First number is 420, second
number is 69.

Pizza is not a valid operator. Alright everybody, so that is a very simple Python
calculator program you can make as a beginner. Hey there, it's me again.

In today's topic, we're going to create a weight converter program in Python. This
is an exercise that will follow up the lesson on if statements. We'll convert
pounds to kilograms, or kilograms to pounds.

The user is going to decide. We will begin by creating a weight variable. We will
assign some user input.

Enter your weight. We will convert this input into a floating point number. So add
that cast.

Then we will ask for a unit. Is this weight in kilograms or pounds? Input kilograms
or pounds. We want the user to type in either K for kilograms or L for pounds.

These are capital letters by the way. Using an if statement, let's first check to
see if our unit is equal to a capital K. That means the current weight is in
kilograms. We need to convert that weight to pounds.

Let's reassign weight equal to our weight times 2.205. Else if unit is equal to L,
we need to convert to kilograms. Weight equals weight divided by 2.205. Else the
user did not type in something that was valid. Let's print using an if string.

Unit was not valid. At the end of our program, we will print the new weight. I'll
use an if string.

Your weight is our new weight after it's reassigned. Now we need a unit of
measurement. This is what I'm thinking we'll do.

Within our if and else if statements, let's reassign our unit. We're reassigning
unit to be LBS for pounds. Else if unit equals KGS for kilograms.

In our results, we will display our new unit. Let's take a look. Enter your weight.

Actually, I'm just going to make one change. I'm going to add colon space. There,
that's much better.

Enter your weight. Let's say I'm 180 pounds. This is in pounds.

I'll type capital L. Your weight in kilograms is 81.63. I think I'm going to round
this. I will enclose the weight variable within a round function. We will round to
one decimal place.

Let's try this again. Enter your weight. Maybe I'm 81 kilograms.

I'll type K for kilograms. Your weight is 178.6 pounds. Let's make sure that this
else statement works too.

Enter your weight. 180 pizzas. Pizzas was not valid.

We're still displaying our output. We would want to avoid that if somebody doesn't
type in a valid unit. Let's cut this line.

Then paste each within the if and else if statements. When we exit the else
statement, we're not printing the output. Let's make sure that this works.

Enter your weight. I am 180 pizzas. Pizza was not valid.

Alright everybody. Well, that is a weight converter program in Python. I thought


this would be a helpful exercise now that we have finished the section on if
statements.

And yeah, that is a weight converter program in Python. Hey everybody. In this
topic, we're going to create a temperature conversion program as an exercise.

We'll begin by asking what the current unit of measurement is. Unit equals. We'll
accept some user input.

Is this temperature in Celsius or Fahrenheit? C slash F. Then we will ask for the
temperature. I'll store the temperature in a variable named temp, meaning
temperature. temp equals input.

Enter the temperature. Then we should cast our user input as a floating point
number. If unit is equal to C. I'll fill this in momentarily.

I'm just going to write pass as a placeholder. Else if unit is equal to F, we will
do something else. Else, let's print something.

Just an error message of some sort. Using an F string. Unit is an invalid unit of
measurement.

Let's test this else statement. Is the temperature in Celsius or Fahrenheit? What
if I were to type K for Kelvin? I'll make up some temperature like 100. K is an
invalid unit of measurement.

Alright, we know the else statement works. Let's convert Fahrenheit to Celsius
using this formula. We will take our temperature equals 9 times our temp divided by
5 plus 32.

I'll take all of this and use the round function. We'll round to one decimal place.
Then we will print the current temperature in Fahrenheit.

I'll use an F string. The temperature in Fahrenheit is our temp variable degrees
Fahrenheit. Let's test this if statement.

Is the temperature in Celsius or Fahrenheit? It is currently in Celsius. What is 33


degrees in Celsius converted to Fahrenheit? The temperature in Fahrenheit is 91.4
degrees. Alright, so this section is working.

Let's work on the else statement. Else, if our unit is currently in Fahrenheit,
we'll convert to Celsius. That formula is temp equals our temperature minus 32
times 5 divided by 9. Then I will round the result to one decimal place.

Then we'll print the temperature in Celsius. The temperature in Celsius is temp
degrees C for Celsius. Is the temperature in Celsius or Fahrenheit? It is currently
in Fahrenheit.

Enter the temperature, 91.4. The temperature in Celsius is 33.0 degrees Celsius.
Well everybody, that is a simple temperature conversion program in Python. Alright
people, we're talking about logical operators today.

Logical operators allow us to evaluate multiple conditions. We can link them


together. There's three we'll discuss.

OR and NOT. We'll begin with OR. With OR, we can check more than one condition.

If at least one of those conditions is true, then the entire statement is true.
Here's an example. Let's say we have an outdoor event.

And I will create two variables. One, temp, meaning temperature. Let's say that
this is in Celsius, 25 degrees Celsius.

Pick Fahrenheit if you would like. And I will create a Boolean variable of
isRaining. I will set that to be false.

It is currently not raining. If the temperature is too hot, too cold, or it's
raining, then I will cancel this outdoor event. We'll write an if statement to
check that.

If our temp, short for temperature, is greater than, let's say, 35. 35 degrees
Celsius. Then I'll use the OR logical operator.

OR if our temp is less than zero. OR if isRaining is true. If one of these


conditions is true, we're going to cancel our outdoor event.

So let's print the following. The outdoor event is cancelled. Else, we will print
something else.

The outdoor event is still scheduled. The temperature is reasonable, and isRaining
is false. It's not raining.

So we print the else clause. The outdoor event is still scheduled. What if the
temperature was really hot, like 36 degrees Celsius? Well, the outdoor event is
cancelled.

What if it's cold? Negative 5 degrees Celsius. The outdoor event is cancelled. This
condition was true.

Therefore, we execute the if statement. Or what if the temperature is reasonable,


but it's raining? isRaining is true. Well, then the outdoor event is still
cancelled.

So with the OR logical operator, at least one of these conditions needs to be true.
If one of these conditions is true, you could consider the entire statement true.
Now let's cover AND.

With AND, we can link two conditions together. Both conditions must be true in
order for that entire statement to be true. So again, let's say we have temp short
for temperature.

And we have a Boolean variable of isSunny. I will set that to be true. We will
check if our temp is greater than or equal to 28 degrees Celsius.

And isItSunny is sunny. If it's hot, and if it's sunny, if this is true, let's
print the following. It is hot outside.

For fun, I'm going to add an emoji, but you don't have to. I just think it's more
entertaining that way. But you do you.

And I will print, it is sunny. Sometimes these emojis are formatted differently.
I'm just going to copy it from somewhere else.

That's better. Currently the temperature is 25, 25 degrees Celsius, and it's sunny.
This condition was false, but this one is true.

With the AND logical operator, both conditions must be true in order for us to
execute this block of code. If our temperature was 30, 30 degrees Celsius, well
then, both conditions are true. It is hot outside, and it is sunny.

Let's write a few more. Let's add elseif. Else if the temp is less than or equal to
zero, and is sunny, we will print something else.

It is cold outside. I'll change the emoji. And it is sunny.

Let's set the temperature to be negative five degrees Celsius. It is cold outside,
and it is sunny. Both these conditions are true, so we do this instead.

You can link as many conditions together as you would like. Let's see if our
temperature is within a certain range. Else if temp is less than 28, and our temp
is greater than zero, and is sunny.

To check to see if something is within a certain range, there is a shortcut too.


PyCharm is recommending this. We can simplify chain comparisons.

So this effectively does the same thing. If 28 is greater than our temp, and our
temp is greater than zero, and it's sunny, then we will print it is warm outside,
rather than hot. And it's still sunny.

So let's say our temperature is 20 degrees Celsius, and it's sunny. It is warm
outside, and it is sunny. Now we have the not logical operator.
It inverts the condition. We are checking to see if something is either not false
or not true. So let's check to see if it's not sunny.

Really I'll just copy what we have and paste it. Else if not is sunny, then that
means it's cloudy. And let's use a cloud emoji.

So basically not does the opposite of what you're looking for. We are checking if
not is sunny. If sunny is false, then this condition is true.

Okay, let's say our temp is 28, is sunny, is now false. It is hot outside, it is
cloudy. What if our temperature was zero? It is cold outside, it is cloudy.

What if the temperature was reasonable, like 20 degrees Celsius? It is warm


outside, it is cloudy. So not, it inverts the condition. If it's true, it's now
false.

If it's false, it's now true. Alright everybody, so those are logical operators.
They allow us to evaluate multiple conditions.

With or, at least one condition must be true. With and, both conditions must be
true. And not, not does the opposite.

It inverts the condition. We check if something is not false or not true. And well
everybody, those are logical operators in Python.

Hey everybody, so today I got to explain conditional expressions in Python. A


conditional expression is a one-line shortcut for using an if-else statement. If
you're familiar with other programming languages, this is also known as the ternary
operator.

It behaves similarly. Using conditional expressions, we can print or assign one of


two values based on a condition. Here's the formula.

Return x if our condition is true, else return y. Here's a basic example. We will
create a variable for number, just num. Let's say our number is 5. I'm going to
print.

Then within our print statement, I will write a conditional expression following
this formula. I'll just copy and paste it. Let's check to see if our number is
positive.

Let's print the text, positive if our condition. What are we checking? Let's check
to see if num is greater than 0. That means it's positive. If this condition is
false, we will instead print whatever comes after else.

Else, negative. Number is 5, that will print positive. If our number was negative
5, well this condition would be false.

We would instead print negative. Let's go over another. Let's check to see if our
number is even or odd.

Let's set num to be 6. This time I will assign the result to a variable. Our result
equals, take our formula. Let's assign even if our num modulus 2. Is our number
divisible by 2? Does that equal 0? Else, return, odd.

Then let's print our result. Result. Number is 6, that is even.

If it's 5, then it's odd. Assign even if our number is divisible by 2. Else,
return, odd. Let's create variables a and b. a will equal 6, b will equal 7. Let's
create a variable of max num.

Equals, follow our formula again. Return variable a if a is greater than b. Else,
return b. Between a and b, which is the maximum number? That would be b of 7. Let's
find the minimum this time. Min num, a if a is less than b. Else, return b. The
minimum number between 6 and 7 is 6. This time we'll take an age.

Age equals 25. We will create a variable of status. Equals, use our formula again.

Return a string of adult if our age is greater than or equal to 18. Else, return a
string of child. Then we will print our status.

Our age is 25, that's greater than or equal to 18. We will print adult. If our age
was 13, then we are a child.

We will instead return child. Let's work with the temperature. Temperature equals
30 degrees Celsius.

So that's hot. Let's create a variable of weather. Assign a string of hot if our
temperature is greater than 20.

Else, we will return a string of cold. What's the weather outside today based on
the temperature? It is hot. If our temperature was 20, then the weather is cold.

Okay, last example. We will work with a user role. I will set this to be admin.

We will define a variable of access level. Equals, again follow our formula. Return
the text of full access if our condition of user role is equal to a string of
admin.

Else, we will return limited access. Our user role is an admin. Let's print our
access level.

And we have full access. But what if we were a guest? Well then we have limited
access. Alright everybody, those are conditional expressions.

They're a one line shortcut for the if else statement. It's similar to the ternary
operator in other programming languages. Using conditional expressions, we can
print or assign one of two values based on a condition.

You follow the formula of return x if our condition is true. Else, return y if it's
false. And well everybody, those are a few examples of conditional expressions in
Python.

Hey everybody, in this topic, I'm going to cover a few useful string methods that
you may be interested in. Then at the end of this video, we will work on an
exercise where we will validate some user input. As we know, a string is just a
series of characters.

Let's ask a user for their full name. Name equals input. Enter your full name.

The first method I'll show you, well technically this is a function. The length
function will give us the length of a string. How many characters is it? We will
find the length of our variable name.

After the user types in some input. This function returns an integer. I'll store
that result within a variable.

Let's just say result. Then I will print whatever the result is. Why don't you go
ahead and type in your full name.

The length of this string, in my example, is 8 characters. That does include spaces
too. 1, 2, 3, 4, 5, 6, 7, 8. If you ever need the length of a string, there is the
length function.

Let's move on. If we were to type our variable name followed by a dot. We have
access to a whole bunch of different methods.

We have the find method. The find method will return the first occurrence of a
given character. The position.

Let's find any spaces. I'll store the result within a variable named result. I will
type in my full name.

The first occurrence of a space, that's what we set, is at position 3. When working
with indexes, we always begin with 0. This first character would have an index of
0. Then 1, 2, 3. That's why the find method returned 3 in place of 4. Let's find
the first occurrence of a capital B.

(Transcribed by TurboScribe.ai. Go Unlimited to remove this message.)

You might also like