Getting started with Python
Since its creation in 1991, Python has one of the most popular programming languages in the world. And, because it’s incredibly versatile and easily accessible, it’s used for everything from making web apps to analysing data. But recently, it’s become the code du jour for coding machine learning applications.
The code itself is simple to use, emphasising readability on the screen (so, expect to see plenty of whitespace when you’re using it). We’ll show you exactly how to master Python in our Microsoft Learn course ‘Introduction to Python.’
Running the code
Python executes code in a runtime environment or interpreter in two different ways.
Script mode is exactly as it suggests – it sees you create a set of commands in a text file (don’t forget to save it with the .py file format). You can then direct the Python interpreter to execute the code line by line, which is then displayed on your screen. Interactive mode, on the other hand, is ideal for testing and debugging. Every command you type is instantly executed, giving you the chance to test out each line of code you write.
Python’s Variables and basic data types
Variables are key to success with Python. Without them, you wouldn’t be able to build anything – they’re that important to the language, letting you create ‘containers’ where different data values can be stored. The most common data types used, and best for those starting out, are numbers and strings.
Let’s break down the example x = 1.
The variable in this instance is X. The equal sign plays the role of ‘assignment operator’, which assigns the numeric value of 1.
Another data type you may come across is Boolean Type, which displays values are either ‘True’ or ‘False’. But now we’re lumbering into a shadowy grey area; technically, Boolean types are examples of numeric integers, with True and False representing the numbers 1 and 0.
Strings are slightly different, relying on alpha values. As such, you can recognise a string data type because it’ll look something like this: x = ‘This is a string’
This is a very typical example of a string. But it also has the power to combine other strings using the + symbol, in a process known as ‘concatenation.’
Or, to put it another way…
Python = ‘Real’ + ‘Versatility’
Many times, particularly in apps and websites, your users are going to need to use the keyboard. To make sure this can happen, you need to use the ‘input’ code.
Let’s say that your user needs to enter their name – you’d program the following:
name = input(‘Enter your name:’)
This also works when, say, your user needs to add a telephone number, which would look like x = input(‘Enter a number: ‘)
You’ll notice something here – this arrangement is, again, using variables. But where you’d typically assign a value to ‘x’, but adding the ‘input’ command, you’re allowing users to assign their own value – in this case, a phone number.
To then display this keyboard input on screen, you’d add the following beneath the ‘input’ command print(name).
This lets you build a command that looks something like…
<eprint('What is your name?') name = input() print(name) print('What is your number?') x = input() print(number)
And that’s the Python overview. You’re now ready to embark upon Microsoft Learn’s Introduction to Python course and start creating your very own apps.