Tuesday, May 19, 2020

What is Python and how do you get started?

what is pythonwhat is python

Let's start from the beginning: what is Python and why should you learn it?

Python is one of the world's most popular programming languages. It powers a huge number of extremely influential apps and websites, including Instagram, Google, Spotify, and Netflix. Python is also commonly used in data science and machine learning, which makes it a very "future-proof" language and one that is likely to stay in-demand for a long time.

Python powers a huge number of extremely influential apps and websites, including Instagram, Google, Spotify, and Netflix.

Despite its clear power and flexibility though, Python is also one of the most beginner-friendly programming languages you're likely to come across. Python serves as a fantastic "gateway drug" into the world of coding, and offers a gentle introduction to higher-level concepts such as object-oriented programming.

Python is also one of the most beginner-friendly programming languages.

Development on Python began in the 1980s, led by Guido van Rossum at Centrum Wiskunde & Informatica in the Netherlands. This was very much Guido's brainchild, and he even dubbed himself the language's "Benevolent Dictator for Life" (BDFL) though he would step down from this role in 2018, passing responsibility on to the Python Steering Council instead.

Also read: How to become a data analyst and prepare for the algorithm-driven future

What is Python in programming terms? Python was conceived as an alternative to the ABC language. It is an interpreted, dynamically typed, garbage-collected language that supports numerous paradigms (object-oriented, procedural, functional).

If you're just starting out, you don't need to worry about any of this. Just know that Python is relatively simple to learn, but also highly in-demand and very powerful. Let's take a look at how to get started with it and how to build your first, very simple, app.

How to get started with Python

First things first, you will need to download some software to use to start programming in Python.

If you're on a desktop computer, that means two things:

  • A Python interpreter
  • A Python IDE

What is a Python interpreter? This is the software that reads the Python code and runs it. Installing an interpreter is like teaching your computer how to speak a foreign language.

python codingpython coding

The IDE meanwhile, is the "Integrated Development Environment." This is the program that you will use to actually type your Python code into. You can save and open files this way, and all on the interpreter when you want to run it. This is your interface for Python development.

When installing an interpreter, you need to decide whether you're going to choose Python 2 or Python 3. Each version has pros and cons, but Python 2 is no longer officially supported, making Python 3 the future-proof choice.

(If you were wondering "what is Python not so good for" one answer is that it is fragmented in this manner, which can present a little bit of confusion getting started!)

Download the latest Python interpreter here:

Note that you may already have a Python interpreter installed, especially if you are running MacOS or Linux.

When it comes to the IDE, there are a number of good options to choose from. Some of these are free, others will cost money but offer advanced features. Some good options include:

PyCharm is free and is among the most popular options for Python development. It is the tool I recommend for most users. That said, it can be a little complex to set up, so be sure to follow the official documentation here.

On mobile, things are simpler because the IDE and interpreter are built into a single app. This is a great way for beginners to get started.

To start coding on mobile, you'll find a single app and download it. Two good examples for learning the ropes are:

There are other versions available with a range of payment models. Both these are good choices for getting started free though.

Once you have any of these things installed, you're ready to write your first Python program!

Python 3: Hello World

It is tradition when learning any new programming language, to start by writing a piece of code that simply writes "Hello World" to the screen. To that end, you will need to use the following code:

Print("Hello World")

Now hit "Play" and you should see the text appear on the screen.

Python programmingPython programming

Side note: If you were using Python 2, you wouldn't need the brackets.

Let's move quickly onto the next lesson: what is a variable in Python?

A variable is like a container that can be used to represent a number or a piece of text. We define this in the code by simply writing a word and then giving it a value.

For example, you could say:

MyVariable = "Hi there!"    Print(MyVariable)

You will see the message "Hi there!" appear on the screen. Notice that you don't need the quotation marks to print a variable, quotation marks are interpreted literally.

A group of letters like this is known in programming as a "string." This is one type of variable, but there are many others. Another type of variable is an integer. This is a variable that represents a whole number.

So we could also say:

MyVariable = 3    Print(MyVariable)

Which would print the number 3 onto the screen!

Some other languages would require you to specifically state what type of variable you wanted to use (String MyVariable = "Hello!"), but in Python, the interpreter figures that out from context. Part of the reason this is possible is that Python uses a smaller number of variable types as compared with, say, Java. There are no "Booleans" in Python for example.

While we won't need to deal with other data types right now, you will eventually encounter the following variables in Python programming:

  • Numbers
    • Integers
    • Floats
    • Longs
    • Complexes
  • Strings
  • Lists
  • Tuples
  • Dictionaries

Manipulating data in Python

Why would you ever want to use a variable? Because it makes your code dynamic. It means that you can alter the way your program behaves depending on the action of the user, and depending on various other factors.

Try running this code and see what happens:

MyVariable1 = 2    MyVariable2 = 20    Print(MyVariable1 * MyVariable2)

Here's a clue: in computer-talk, the * symbol represents multiplication.

You can also combine strings in interesting ways:

FirstName = "Bill"    LastName = "Gates"    FullName = FirstName + "  " + LastName    print(FullName)

Your first Python 3 app!

So now we've answered the question "what is Python" and we've had a go at writing some basic code.

What about making something that a person might actually be able to use and have fun with?

Also read: Kotlin vs Java for Android: Key differences

To do this, we're going to need to let the user interact with the program. That means we need to handle input.

Try this:

UserName = input("Please enter your name: ")    Print("Hello " + UserName)

You can probably figure out what is going on here! When you use the command "input," Python will prompt the user with the text in the brackets, and then wait for the response. That string will then be referred to as UserName.

Note: Python 2 uses raw_input instead of input.

Run Python on AndroidRun Python on Android

Now we have inputs, the ability to talk to the user, and even some basic math. How about we put this together in a fun little app? This one will tell you how long you have until you are 100 years old!

UserAge = input("How old are you? ")    YearsTo100 = 100 - int(UserAge)    print("In ", YearsTo100, "years, you'll be a hundred!!")    print("That is ", int(UserAge) * 360, " days! Or ", (int(UserAge) * 360) * 24, " hours. ")

Conditional statements

Android Studio developmentAndroid Studio development

There is one last trick that I want to share with you before we go: using conditional statements.

A conditional statement is a command that only runs under certain conditions. This usually means checking the value of a variable first.

To use a conditional statement in Python, you use the "If" statement, followed by an indentation.

For example:

UserName = input("Please enter your name:")    print("Hello " + UserName)    if UserName == "Adam":                    print("Admin mode enabled")     print("What would you like me to do now?")

In this program, the indented code will only run if the UserName given is Adam. Note that when checking a value as opposed to assigning one, we use two = signs rather than one.

Here then, the user will be asked what they want to do next whoever they are – but only I will be granted admin status. Or other people called Adam.

We're just scratching the surface of Python can do

Using these basic commands and lessons, you can actually do an awful lot already. You could make a quiz, a calculator, a simple database, and more! To really flex the full power of Python though, you'll need to understand concepts such as functions, modules, and more. To that end, we recommend checking out our guide to the best online Python courses. 

That said, if you're a true beginner and looking for a great course that's easy to get started with, we highly recommend Coding with Python: Training for Aspiring Developers, which you can nab for just $49.99, which is an absolute steal as the course is valued around $700.

$49 .99
Coding with Python: Training for Aspiring Developers Bundle
Save $641 .01
Buy it Now
Coding with Python: Training for Aspiring Developers Bundle Buy it Now
Save $641 .01 $49 .99


from Android Authority https://ift.tt/2XguRvZ
via IFTTT

No comments:

Post a Comment