Intro To Python Programming
Before you can start programming with Python, there are a number of general programming fundamentals that are important to understand. Let’s take a closer look at the basics.
Programming Language Basics
Programming languages tend to share some foundational concepts and general building blocks. By learning these concepts, we can gain a better understanding of any new programming language that we might come across in the future.
With that being said, it is very important to have a solid understanding of the basics of programming languages before starting to write code. In most cases, only the process of writing code and executing it will change between individual languages. Let’s dive into these building blocks and first deepen our understanding of syntax and semantics:
Syntax defines a set of rules and a combination of various symbols. Syntax is followed to write properly structured code that can be accepted by the language, before it is issued as instructions for the computer to run. Programming languages can vary in the syntax used, and code is written in a document with a particular file extension (e.g. for Python language code is written in a file with a '.py' extension). If the code written is not valid syntactically, the document will show “syntax error”. We’ll explore Python syntax in the upcoming lessons.
The semantics of a language are responsible for defining the meaning behind the syntax. In other words, it’s not enough to write a sentence with the proper punctuation and grammar, and order of words if it doesn’t mean anything. For example, the sentence “The desk ate the color blue.”, is syntactically correct, but doesn’t have any meaning. The same goes for programming. While code might be written with the right syntax in mind, the end result has to mean something.
For our purposes, semantics describes a set of processes a computer will go through while executing the code in a particular language. This could be determining what output a program should produce for a given set of inputs or it could be just describing the process of how a code will be run on different platforms. Semantic analysis takes any code written and determines whether or not the code consists of valid strings. If not, the code would not be run and meaningful errors will be thrown to tell the programmer that the code contains errors.
In this lesson, we will go through a quick introduction to Python as a programming language. Note: We will be using Python 3 for programming in Python
How to Write Python Code
While Python is one of the most beginner-friendly programming languages in use, it’s a good idea to understand some core principles before getting started. Python programming courses, for example, have grown in popularity, as they can quickly get you up and running with the language.
It’s also a good idea to take advantage of the wealth of material available online. There are a number of PEP documents, for example, which can be invaluable for beginners. PEP stands for Python Enhancement Proposal, and these documents describe new features and aspects of Python, including documents, design, and style.
PEP 8 is a readily-available document that aims to improve the readability and consistency of Python code – it also provides guidelines and best practices on how to write Python code.
Expressions or Statements
An expression or a statement is a line of code or building block that is syntactically correct and is supposed to carry out a certain action when executed. In the last lesson, we typed
2 + 2 in our notebook. In this case,
2 + 2 is an expression or statement that when executed, will result in an action of performing the addition of two numbers. The outcome of the action will be the result - in this case,
Indentation, also referred to as leading whitespace, is an important element of Python. The way that lines of code are indented in Python determines how statements are grouped together.
PEP 8 includes two key indentation rules:
- Make sure to use four consecutive spaces to indicate indentation.
- Use spaces instead of tabs.
There are many mathematical operators that Python provides that are useful as part of writing code. Addition
/, Modulus or Remainder
% and Power or "Raised To"
** are some of the mathematical operators that Python provides to developers. Type the following code one line at a time and run the cell each time.
Kick-Start Your Data Career
We offer a wide variety of programs and courses built on adaptive curriculum and led by leading industry experts.
- Work on projects in a collaborative setting
- Take advantage of our flexible plans and scholarships
- Get access to VIP events and workshops
The Python certificate course provides individuals with fundamental Python programming skills to effectively work with data.
Taught by data professionals working in the industry, the part-time Data Science course is built on a project-based learning model, which allows students to use data analysis, modeling, Python programming, and more to solve real analytical problems.
The Data Science bootcamp is an intensive course designed to launch students' careers in data.
The part-time Data Analytics course was designed to introduce students to the fundamentals of data analysis.