Learning Python: Control Statements

Introduction

Control statements in Python differ slightly from control statements in languages such as Java, C#, and JavaScript.

Structure

The structure of how to write if/else if/else control statement in Python is as follows:

if [insert condition here (no brackets)]:
    [insert some action (no brackets)]
elif [insert condition here (no brackets)]:
    [insert some action (no brackets)]
else:
    [insert some action (no brackets)]

We can also do a ternary operator as follows:

name = "John"
valid = True if name == "John" else False

This blog post was originally published on my blog Communicode, where I write about different tech topics.

CoduPython
Avatar for Muhammad Asfour

Written by Muhammad Asfour

I am a Full Stack Developer with over 5 years of experience. I have worked with different tech stacks such as: Groovy/Java, PHP, .NET/.NET Core (C#), Node.js, React, Angular, and Vue.

Loading

Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.