Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Introduction to if Statement | Python if Statement
Conditional Statements in Python
course content

Course Content

Conditional Statements in Python

Conditional Statements in Python

1. Python if Statement
2. Python if-else Statement
3. Python if-elif-else Statement
4. Python Ternary Operator

bookIntroduction to if Statement

Hello, everyone! I'm delighted to welcome you to this course. Here, we will become familiar with the conditional operator, sometimes referred to as the branch operator.

Let's clarify a bit:

Here, we have the following condition: age > 18. In our case, age = 20, and because 20 is greater than 18, the condition is True. So, the code inside the if block is run, and we see the message "You can vote". We learned a little bit about the conditional operator, and now let's strengthen our knowledge with simple practice. In the next chapter, we will study the syntax of this construction in more detail.

Example:

123
age = 20 if age > 18: print('You can vote')
copy

Analysis:

In this case, we have the following condition age > 18. In our case, age = 20, and since 20 > 18, the condition is True, so the code that is written in the if statement block is executed, and as a result we see "You can vote". We got a little acquainted with the conditional operator, let's strengthen our knowledge with simple practice, and in the next chapter, we will study the syntax of this construction in more detail.

Task
test

Swipe to show code editor

  1. You have the following string: string = 'codefinity';
  2. Count the length of this string and write the result to the variable len_of_string;
  3. Write if statement that checks for the following condition: if the string length is greater than 7 , print the following text: Codefinity is the best platform to start your career in IT.

Fill in the blanks in the code you've already been given.

Once you've completed this task, click the button below the code to check your solution.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 1
toggle bottom row

bookIntroduction to if Statement

Hello, everyone! I'm delighted to welcome you to this course. Here, we will become familiar with the conditional operator, sometimes referred to as the branch operator.

Let's clarify a bit:

Here, we have the following condition: age > 18. In our case, age = 20, and because 20 is greater than 18, the condition is True. So, the code inside the if block is run, and we see the message "You can vote". We learned a little bit about the conditional operator, and now let's strengthen our knowledge with simple practice. In the next chapter, we will study the syntax of this construction in more detail.

Example:

123
age = 20 if age > 18: print('You can vote')
copy

Analysis:

In this case, we have the following condition age > 18. In our case, age = 20, and since 20 > 18, the condition is True, so the code that is written in the if statement block is executed, and as a result we see "You can vote". We got a little acquainted with the conditional operator, let's strengthen our knowledge with simple practice, and in the next chapter, we will study the syntax of this construction in more detail.

Task
test

Swipe to show code editor

  1. You have the following string: string = 'codefinity';
  2. Count the length of this string and write the result to the variable len_of_string;
  3. Write if statement that checks for the following condition: if the string length is greater than 7 , print the following text: Codefinity is the best platform to start your career in IT.

Fill in the blanks in the code you've already been given.

Once you've completed this task, click the button below the code to check your solution.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 1
toggle bottom row

bookIntroduction to if Statement

Hello, everyone! I'm delighted to welcome you to this course. Here, we will become familiar with the conditional operator, sometimes referred to as the branch operator.

Let's clarify a bit:

Here, we have the following condition: age > 18. In our case, age = 20, and because 20 is greater than 18, the condition is True. So, the code inside the if block is run, and we see the message "You can vote". We learned a little bit about the conditional operator, and now let's strengthen our knowledge with simple practice. In the next chapter, we will study the syntax of this construction in more detail.

Example:

123
age = 20 if age > 18: print('You can vote')
copy

Analysis:

In this case, we have the following condition age > 18. In our case, age = 20, and since 20 > 18, the condition is True, so the code that is written in the if statement block is executed, and as a result we see "You can vote". We got a little acquainted with the conditional operator, let's strengthen our knowledge with simple practice, and in the next chapter, we will study the syntax of this construction in more detail.

Task
test

Swipe to show code editor

  1. You have the following string: string = 'codefinity';
  2. Count the length of this string and write the result to the variable len_of_string;
  3. Write if statement that checks for the following condition: if the string length is greater than 7 , print the following text: Codefinity is the best platform to start your career in IT.

Fill in the blanks in the code you've already been given.

Once you've completed this task, click the button below the code to check your solution.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Hello, everyone! I'm delighted to welcome you to this course. Here, we will become familiar with the conditional operator, sometimes referred to as the branch operator.

Let's clarify a bit:

Here, we have the following condition: age > 18. In our case, age = 20, and because 20 is greater than 18, the condition is True. So, the code inside the if block is run, and we see the message "You can vote". We learned a little bit about the conditional operator, and now let's strengthen our knowledge with simple practice. In the next chapter, we will study the syntax of this construction in more detail.

Example:

123
age = 20 if age > 18: print('You can vote')
copy

Analysis:

In this case, we have the following condition age > 18. In our case, age = 20, and since 20 > 18, the condition is True, so the code that is written in the if statement block is executed, and as a result we see "You can vote". We got a little acquainted with the conditional operator, let's strengthen our knowledge with simple practice, and in the next chapter, we will study the syntax of this construction in more detail.

Task
test

Swipe to show code editor

  1. You have the following string: string = 'codefinity';
  2. Count the length of this string and write the result to the variable len_of_string;
  3. Write if statement that checks for the following condition: if the string length is greater than 7 , print the following text: Codefinity is the best platform to start your career in IT.

Fill in the blanks in the code you've already been given.

Once you've completed this task, click the button below the code to check your solution.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 1. Chapter 1
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt