DEV Community

Pedro Alarcon
Pedro Alarcon

Posted on • Updated on

Intro to Python

INTRODUCTION

So what exactly is Python and what does it do? Python is one of the more popular programming languages, and was created by Guido Van Rossum, and released in 1991. The main uses for using Python is web development (server-side), software development, mathematics, system scripting. Python is a very high-level, general-purpose programming language. Its design philosophy really emphasizes code readability with the use of significant indentation. This helps for navigating through such large chunks of code and understanding what your reading. Like many other programming languages Python has gone through numerous updates since its official release in February 1991. Python claims to strive for a simpler, less-cluttered syntax and grammar while giving developers a choice in their coding methodology, Python embraces a "there should be one—and preferably only one—obvious way to do it." philosophy.

SYNTAX

Python is meant to be one of the more easily readable languages, But what exactly is syntax? The syntax of the Python programming language is the set of rules that defines how a Python program will be written and interpreted. While using indentation, english keywords, and fewer syntactic exceptions. Making writing code more understandable but even easier to remember. Python is even considered one of the more easier programming languages to learn as a new developer. For example lets talk about python indentation which refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important. Python uses indentation to indicate a block of code.

Syntax example 1

This line of code is written correctly, the only thing its missing is indentation. Python will give you an error if you skip the indentation.

Syntax example 2

So lets fix this, The number of spaces is really up to you as the programmer, the most common use is four, but it has to be at least one.

Syntax example 3
As you can see in the example above the function was fixed because we implemented indentation. Overall Python's syntax is designed to be clear and easy to understand, which makes it accessible for beginners and reduces the cost of program maintenance. Also Python is executed line by line by the Python interpreter. This makes development so much better for debugging and makes it easier to immediately see the results of your code.

FUNCTIONS

A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result. To create a function in Python you must define the function using the DEF keyword.

Function example 1

In the example above you can see we declared a function using the def keyword and called the function as well. We can also still write functions taking in parameters.

Function example 2

In this example, the function greet takes one parameter name, which represents the name of the person to greet. When you call the function, you pass a value for the name parameter, and the function prints a greeting using that value.

Object-Oriented Programming

Object-oriented programming (OOP) is a method of structuring a program by bundling related properties and behaviors into individual objects. For example, an object could represent a person with properties like a name, age, and address and behaviors such as walking, talking, breathing, and running. OOP models real-world entities as software objects that have some data associated with them and can perform certain operations. It aims to implement real-world entities like inheritance, polymorphisms, encapsulation, and Abstraction.

  • Abstraction in Object-Oriented Programming (OOP) is like hiding the complicated stuff and showing only what's important. For example if you have a remote, You don't need to know how all the circuits and buttons inside it work. All you need to know are the buttons you press to change channels, adjust the volume, and turn it on or off. That's abstraction!

  • Encapsulation in Object-Oriented Programming (OOP) is like putting things in a box and only letting certain people access them. For example encapsulation works similarly. You have a class, which is like the treasure chest, and it contains data (like variables) and methods (like functions) that operate on that data. You "lock" the class by making some parts private, meaning they can only be accessed from within the class itself. Other parts are public, which means they can be accessed from outside the class.

  • Inheritance in Object-Oriented Programming (OOP) is like passing down traits from parents to children. like creating new classes based on existing ones and the child inherits things from the parent class.

  • Polymorphism in Object-Oriented Programming (OOP) is like having one name with many forms. For example, both a "Cat" class and a "Dog" class might have a "makeSound()" method. When you call "makeSound()" on a cat object, it meows, and when you call it on a dog object, it barks.

Overall OOP helps you write code that's organized, reusable, and easy to understand. It's like building with Lego blocks, where each block (object) has its own shape, color, and function, and you can combine them to create something bigger and more complex.

Conclusion

Python is a widely used programming language, favored for its simplicity, versatility, and readability. It's syntax, emphasizing code readability with significant indentation, English keywords, and fewer syntactic exceptions, simplifies learning and maintenance. Functions enable encapsulation of code blocks for reusability and organization, while Object-Oriented Programming (OOP) structures programs around objects, facilitating abstraction, encapsulation, inheritance, and polymorphism. Abstraction hides complex details, encapsulation bundles data with methods for controlled access, inheritance promotes code reuse, and polymorphism allows objects of different classes to be treated interchangeably.

Resources

https://en.wikipedia.org/wiki/Python_(programming_language)
https://www.w3schools.com/python/default.asp
https://www.geeksforgeeks.org/python-programming-language/

Top comments (0)