12th Aug 2021 10 minutes read Python Terms Beginners Should Know – Part 2 Soner Yıldırım python python basics python terms Still learning Python terms? No worries – we're here to help you master the basics of Python for beginners. If you missed Part 1 of Python Terms Beginners Should Know, you can read it here. Why should you spend your time learning Python terms? Well, this general-purpose programming language has experienced tremendous popularity in recent years. There are several reasons that make people from different professions choose Python. First, Python is easy to learn and has an intuitive syntax. Since a substantial amount of software development is about reading other developers' code, it's very important to understand that code. Python syntax is almost as understandable as reading plain English. Second, Python is the go-to language in the data science ecosystem. If you plan to become a data scientist, our Python for Data Science track is a great place to start. Third, Python is flexible; it's not limited to data science. You can create software products in many different areas, such as web development and game development. Want more? Here is a list of 5 reasons to learn Python. Finally, you do not have to be a software developer or experienced programmer to learn Python. LearnPython.com makes it easier to grasp this language by providing several well-designed learning tracks. The Python Basics mini track is a great way to start your learning journey. 10 More Python Terms You Should Know In this article, I will explain 10 Python terms that beginners should know. These terms are simple yet fundamental for learning Python. Once you have a comprehensive understanding of the basics, you can more easily improve your skills and learn advanced topics. The first five terms are related to the concept of object-oriented programming. Since Python is an object-oriented language, these terms will lay the groundwork. They aren't complicated, but they are very important. The first five Python terms on my list are object, class, attribute, instance, and method. For most terms, I will create a simple example in Python so you can see it for yourself. The first five terms are best explained using an analogy. Suppose you have a class called Car. You can create different cars with this Car class. All you need to do is to define the brand and color of the car you want to create. You can also drive your car for as many miles as you wish. As we continue, I'll use this Car class example to illustrate the definitions of the related terms. 1. Object I chose object as the first Python term because everything in Python is an object. Python programs are built around objects. Integers, strings, functions, lists, and tuples are all examples of objects. Each object has a type. How we can use or interact with an object is based on its type. The object types are defined with classes. In the case of the Car class, each car you create or produce is an object of the Car type. 2. Class Class is another core Python term. Classes can be considered as a blueprint for objects. When we talk about the Car class, we mean the blueprint for all the Car objects. Take lists as another example. When you create a list, you actually create an object with the list type. If we create the mylist object shown below and then use the type() function to tell us its type, notice what the function returns: >>> mylist = [1, 2, 3, 4] >>> type(mylist) <class 'list'> That's right – it's a list! One of the advantages of using classes is that you do not need to know how they are implemented. You just need to be able to use the class appropriately in your code. There are many built-in Python classes, but you can also create your own classes. 3. Attribute Attributes define a class. There are two main types of attributes: Data attributes define what is needed to create an object that belongs to a particular class. Methods or procedural attributes explain or describe how to interact with the class objects. Let's say you create a blue BMW car. The color and brand are the data attributes. Then, you drive your car for 100 miles. The driving action is a procedural attribute (i.e. method). 4. Instance An instance of a class is an object that belongs to the class. The type of an instance is defined by the class it belongs to. For example, when you create a list, you actually create an instance of the built-in list class of Python. Going back to our analogy, the cars you create are instances of the Car class. 5. Method Methods are also known as procedural attributes. You can use methods to interact with class instances. Driving a car that belongs to the Car class is a method (i.e. an action). Methods are quite similar to functions, but they belong to a particular class. For example, when you want to add an item to a list, you can use the append() method. Below, we'll use this method to add a number to the end of the mylist list: >>> mylist = [1, 2, 3, 4] >>> mylist.append(5) >>> print(mylist) [1, 2, 3, 4, 5] Thus, append() is a procedural attribute of the list() class. The next step is to see a class in action. Let's create a simple class called Person so we can practice what we have learned so far: class Person(): def __init__(self, age, name): self.age = age self.name = name def age_diff(self, other): diff = self.age - other.age return abs(diff) In the Person class, age and name are the data attributes. You need to specify the values of these attributes to create an instance of this class. The Person class has two methods, __init__ and age_diff. __init__ is a special method that automatically runs when an instance is created; it is also called the constructor method because it is executed every time an object is created. The age_diff method can be used to calculate the difference between the ages of two Person objects. Let's create two Person objects and calculate the age difference. >>> P1 = Person(24, "John") >>> P2 = Person(28, "Jane") >>> P1.age_diff(P2) 4 In the above code, P1 and P2 are objects that are of the type Person. They are also called instances of the Person class. age_diff is a method of the Person class. As you can see in the example above, we can use it to calculate the difference between the ages of two Person objects. 6. Sets A set is one of Python's built-in data structures. Data structures organize data in a certain way and are fundamental to any programming language. In Python, a set is an unordered collection of distinct immutable objects. Sound complicated? Let's break it down. A set must contain zero or more elements that do not possess any order; thus, we cannot talk about the first or last item in a set. Sets contain distinct immutable objects. In other words, you cannot have duplicate items in a set. The elements must be immutable (unchangeable) and can be data types like integers, strings, or tuples. Although the elements can't be changed, the set itself is mutable – we can add new items or remove existing elements in a set. Let's create a simple set by writing the elements in curly braces, as shown below: >>> myset = {1, 5, "John"} >>> type(myset) <class 'set'> If you try to add duplicate items in a set, they will automatically be removed: >>> myset = {1, 5, "John", "John", 5, 2} >>> print(myset) {1, 2, 'John', 5} If you try to create an empty set by typing {}, you'll create a dictionary instead. In that case, type set(). 7. Tuple A tuple is another of Python's built-in data structures. It's a collection of objects, but unlike sets or lists, tuples cannot be changed. Once it's created, we cannot update or modify a tuple. Also, tuples can have duplicate items. You create a tuple by listing items inside parentheses: >>> mytuple = (1, 4, 5, "foo") >>> type(mytuple) <class 'tuple'> One common use case for tuples is with functions that return multiple objects. You can assign the returned items to a tuple. Then, each item can be accessed via indexing or slicing (extracting part of a tuple, list, string, etc.). Here's an example of tuple indexing: >>> mytuple = (102, 14, 15) >>> print(mytuple[0]) 102 >>> print(mytuple[2]) 15 To learn more about Python data structures, see this article on lists, tuples, and sets. 8. String Strings and integers might be the most common data types in Python. Strings are basically chunks of text, although they can store all kinds of characters. Some examples of strings are: a = "John" b = "1dd23" c = "?--daa" Python provides several functions and methods to manipulate and work with strings. For instance, we can access any part of a string by slicing: >>> mystring = "John Doe" >>> print(mystring[:4]) John In slicing, :4 means "start from the first character (index 0) and return everything until you get to the character at index 4". The upper limit is exclusive – it doesn't include that character – so the return string contains the four characters in the index positions 0, 1, 2, and 3. And here's indexing with a string: >>> print(mystring[-1]) e Here, -1 means the last character in the string. Another common operation with strings is splitting. You can split a string at any given character. >>> mystring.split(" ") ['John', 'Doe'] Splitting a string returns a list that contains the sections before and after the split. In this example, we split the string at the space (" "). 9. Package A package is a directory that contains Python scripts. Python offers a very rich selection of packages; any developer can create a package and release it under the Python Package Index (PyPI). Packages are created to automate or expedite certain tasks. For instance, the pandas package provides several functions for efficient data analysis and manipulation. Third-party packages play a critical role in making Python the first-choice programming language in the data science ecosystem. Let's demonstrate a simple use case using the pandas and NumPy packages. We'll use them to create a data frame (which is similar to a database table). Don't worry about understanding the code; just see how few lines of code we need to do this task: import numpy as np import pandas as pd df = pd.DataFrame(np.random.randint(10, size=(4,3)), columns=list("ABC")) df With pandas and NumPy, we created a data frame that contains random integers between 0 and 10 with just a few lines of code! 10. Module A Python package contains many scripts (e.g. small programs that perform a particular task or tasks). Each script in a package is known as a module. Some packages are quite large and contain many modules. Often, you will only need a particular module from that package. Python allows us to import and use modules, as we demonstrated in the NumPy and pandas example above. Why So Many People Are Learning Python Well done! If you've read our earlier article on Python terms, you now know 20 concepts that are central to learning Python. Remember, Python was created to make coding easy for novice programmers and non-programmers. This is the fundamental reason why it has been widely accepted in the data science ecosystem. However, Python covers a broader range of applications. Here is a list of 10 cool reasons to learn Python. Get started today! Tags: python python basics python terms