Python In Tutorial – Complete Guide

Welcome to our comprehensive Python ‘in’ tutorial! As the foundation of many technological marvels and game mechanics you’d encounter every day, Python and particularly the ‘in’ keyword, holds a world of possibilities for you to explore. This knowledge isn’t just interesting, it’s incredibly useful, whether you’re ideating a world-changing app, creating a riveting game, or simply trying to automate some mundane task. We promise you an exciting journey as you go from beginner to Python whizz with this series.

What Is the Python ‘in’ Keyword?

The Python ‘in’ keyword is commonly used for membership checks. It allows us to determine if a particular element or value exists within a data collection or structure such as a list, string, dictionary, or tuple. It’s a powerful keyword that can save you time, energy, and many lines of code.

A proficient coder knows the value of writing clean, efficient, and readable code. The Python ‘in’ keyword is a simple tool that contributes greatly to this purpose. It enables coders to easily perform checks and validation within data structures, simplifying elaborate code and ultimately making it easier to understand and maintain.

Additionally, the ‘in’ keyword is nifty when writing game mechanics, as it helps in seamlessly verifying game states, validating inputs, and much more. If you want to turn your ideas into reality or elevate your Python coding skills, this tutorial is the place to start.

CTA Small Image
FREE COURSES AT ZENVA
LEARN GAME DEVELOPMENT, PYTHON AND MORE
ACCESS FOR FREE
AVAILABLE FOR A LIMITED TIME ONLY

Using ‘in’ with Lists and Tuples

We’ll start by showing how you can use the ‘in’ keyword with Python’s data structures, specifically lists and tuples.

my_list = [1, 2, 3, 4, 5]
print(3 in my_list)  # prints: True
print(6 in my_list)  # prints: False

Here, the ‘in’ keyword checks if the value 3 or 6 is present in the list my_list. The same can be done with tuples:

my_tuple = ('apple', 'banana', 'cherry')
print('banana' in my_tuple)  # prints: True
print('pear' in my_tuple)    # prints: False

Using ‘in’ with Dictionaries

The ‘in’ keyword is also very useful when it comes to dictionaries. Depending on your needs, it can check either keys or values.

my_dict = {'name': 'John', 'age': 30, 'occupation': 'engineer'}
print('age' in my_dict)         # prints: True
print('salary' in my_dict)      # prints: False
print('engineer' in my_dict)    # prints: False

As you can see, by default the ‘in’ keyword checks for keys in dictionaries. The ‘engineer’ check returns False even though it is a value in the dictionary.

If you want to check for the existence of a value in a Python dictionary, leverage the .values() method:

print('engineer' in my_dict.values())  # prints: True

This time, the ‘engineer’ check returns True because we’ve explicitly instructed Python to search for it within the dictionary’s values.

Using ‘in’ with Strings

Let’s not overlook another useful application of the ‘in’ keyword: checking for substrings within a string!

my_string = "Hello, World!"
print("World" in my_string)  # prints: True
print("Zenva" in my_string)  # prints: False

The ‘in’ keyword attempts to find the provided substrings in my_string and returns a boolean value illustrating their presence or absence.

Negation using ‘not in’

Flip the script and use ‘not in’ for verification when a particular element is absent in a data structure:

my_list = [1, 2, 3, 4, 5]
print(0 not in my_list)  # prints: True
print(4 not in my_list)  # prints: False

Similarly, ‘not in’ can verify the absence of keys or values in a Python dictionary:

my_dict = {'name': 'John', 'age': 30, 'occupation': 'engineer'}
print('age' not in my_dict)         # prints: False
print('salary' not in my_dict)      # prints: True
print('engineer' not in my_dict)    # prints: True
print('engineer' not in my_dict.values())  # prints: False

‘in’ keyword in Conditional Statements

The ‘in’ keyword is commonly used in Python’s conditional (if) statements. Here’s how you can do so:

my_list = [1, 2, 3, 4, 5]

if 3 in my_list:
    print("Number 3 is in the list!")
else:
    print("Number 3 is not in the list!")  # prints: Number 3 is in the list!

Surely you’d agree, the ‘in’ keyword is a real timesaver in Python. With concise syntax and powerful functionality, it makes your job as a coder much easier!

Where to Go Next?

Now that you’ve had a taste of the power of Python and the ‘in’ keyword, you’re likely excited to learn more. It can be tricky to know where to go next, but don’t worry – we’ve got just the thing.

Our Python Mini-Degree is a comprehensive program that transitions you from a beginner to an advanced Python programmer. This Mini-Degree includes projects like creating games, building applications, and developing robust AI chatbots. The curriculum incorporates coding basics, algorithms, object-oriented programming, game development, and app development using Python libraries like Pygame, Tkinter, and Kivy. Sounds exciting, right?

One of the special features of our Mini-Degree is that the courses are project-based, which allows you to apply your learning practically. This makes it easier to comprehend concepts while providing you with useful material to showcase in your portfolio.

Our courses are suitable for both beginners and experienced programmers, complete with coding challenges and quizzes to reinforce your learning, while being flexible enough to accommodate different learning preferences and schedules. Additionally, our support team is always ready to help beginners with any queries.

Also, be sure to check out our broader collection of Python courses here.

Conclusion

By making Python’s ‘in’ keyword a part of your coding arsenal, you’re clearing the path to writing cleaner and more efficient code. Its advantages are numerous: validation checks, simplified if statements, and not to mention the invaluable role it plays in the creation of game mechanics, that will make your job as a developer both easier and more enjoyable.

There is no better time than now to dig deeper and discover just how much more Python has to offer. Become a Python champ with Zenva’s Python Mini-Degree, where exciting projects, thorough course materials, and a supportive learning community are part of the package. Harness the power of Python and take the programming world by storm, code by code!

Did you come across any errors in this tutorial? Please let us know by completing this form and we’ll look into it!

FREE COURSES
Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.