Python for Teens: Startup Python Tutorial for teenage

While we can't teach you everything in one blog post, you can get a pretty good start by learning how to write your own programs! In this Python...






Have you ever wondered how a computer works? While we can’t teach you everything in one blog post, you can get a pretty good start by learning how to write your own programs!

In this Python tutorial, you’ll learn the basics of programming a computer using one of the best languages for beginners.

What is a Programming Language? What is Code?

The code is pretty much like writing a paragraph of instruction or creating a to-do list for computers. Unlike us humans, the to-do list and instructions you write for the computer have to be extremely detailed and written in some logic.

With code and programming, you can get the computer to draw complex shapes and create rich computer graphics, and then create programs that understand game mechanics and help you build games that feel real with gravity and particle collision, with these programs you can create the most intense and immersive games of all sorts.

With code and programming, you can create and send content all over the world with your blog and personal website and style your blog to meet your style. You can build tech-driven business solutions and reach a wider range of customers and cater to a wider range of needs.

Furthermore, with code and programming, you can create smart home applications, like an automated pet feeder, a smart mirror, or even create a robot that can help around with household tasks and be your virtual assistant to talk to and understand you. Unlike what many people think, there’s a lot of art involved in computer engineering and computer science.

After you’ve written your program, a compiler converts the code you’ve written into machine code — the lowest-level language of a computer. Machine code instructs the central processing unit, or CPU, what steps to take such as loading a value or performing some arithmetic.

If you’ve ever heard somebody say “I compiled my program”, it means they converted their code into machine code.

There are many types of programming languages and their uses in various works. For example, if you want to learn web development you need to learn HTML, CSS, and JavaScript. Hopefully, you get an idea of what can we do with help of programming languages. In this article, I will tell you how can a teen learn Python effectively and fast way.

Why should a teen learn Python?

The answer is "Python is Beginner Friendly."

There are dozens of programming languages, and many of them are good for a wide variety of projects. Python is unique in that it’s easy to understand, even for kids and teens with no programming experience. We recommend Python for teens because some languages use a perplexing combination of symbols and abbreviations. That's no need in Python.

The Python Program

print "Hello, World!"
...
"Hello, World!"

The Machine Code

c7 3c 2a 3c 2a 2b 2a 5c 3c 28 5c 2a 2b 2a 5c 3c
28 5c 2a 2b 2a 5c 3c 28 5c 2a 2b 2a 5c 3c 28 5c
2a 2b 2a 5c 3c 28 5c 2a 2b 2a 5c 3c 28 5c 2a 2b
2a 5c 3c 28 5c 2a 2b 2a 5c 3c 28 5c 2a 2b 2a 5c
3c 28 5c 2a 2b 2a 5c 3c 28 5c 2a 2b 2a 5c 3c 28
5c 2a 2b 2a 5c 3c 28 5c 2a 2b 2a 5c 3c 28 5c 2a
2b 2a 00 00 01 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 64 48 65 6c 6c 6f 2c 20 57
6f 72 6c 64 21 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
...
"Hello, World!"


It’s quite evident why you don’t want to program directly in machine code. However, there are a few people who do — there’s no accounting for taste! :]

There’s one minor detail we left out above. Python is an interpreted language; you don’t compile it directly into machine code as we alluded to above.

Instead, Python uses something called an Interpreter. An interpreter is yet another program that compiles code into something called Bytecode, which is then converted into machine code as the program runs. You’ll read more about interpreters later.

When you finally run the program, the machine code you recently compiled is loaded into memory and read by the CPU to execute the program.

However, you don’t need to really understand the inner workings of compilers to get started programming in Python, but you should first make sure that you have Python installed.

Python Setup


Let's know about the python setup. Don't be panic. I am talking about a very easy method to set up your Pc/laptop ready to code with python.

Let's get started!!!

No 1. Python IDE

Many PCs and Macs will have python already installed. But if you haven't downloaded it you can download it from here  https://www.python.org/
Note: Make sure you have downloaded the latest version of Python. In this tutorial we will work with Python 3. Make sure you doownloaded 
the latest version.

Now let's check if we downloaded the latest version of Python. Please follow these steps-

 Search in the start bar for Python or run the following on the Command Line   (cmd.exe):

C:\Users\Your Name>python --version
For Linux users open the command line and Mac users open the terminal then write
python --version
Now you are ready to coding.

No 2. PyCharm

Let's know about PyCharm. PyCharm is a dedicated Python Integrated Development Environment (IDE) providing a wide range of essential tools for Python developers, tightly integrated to create a convenient environment for productive Python, web, and data science development.

PyCharm has 2 different versions:
1. Professional
2. Community

We recommend you to use the Community version first then use Professional. One more thing PyCharm is also available on Mac and Linux.

Want to download PyCharm?

No 3. Visual Studio Code

Let's know about our last IDM VS code. Visual Studio Code is a lightweight but powerful source code editor which runs on your desktop and is available for Windows, macOS, and Linux. It comes with built-in support for Python, JavaScript, TypeScript, and Node.

Want to download VS Code?



Variables

Variables are a way of storing data in the memory on a computer; you’ll use them a LOT as you program. In some languages, variables have a specific type which indicates what kind of variable class they belong to.

In Python, you don’t have to declare your variable types. Don’t worry too much about that detail now; you’ll learn more about this a little later in the tutorial.

Type the following command into the interpreter and press Enter:
hello = "Hello World!"

This declares and sets the hello variable to the value Hello World. Now, instead of typing “Hello World” everywhere you need that string in your program, you can just use the hello variable.

Type the following command into the interpreter and press Enter:
print hello

This prints out the same thing as your Hello World example, but instead it’s printing out the value of the hello variable.

Variables can also be used to store numbers. Type the following commands into your interpreter:
x = 5
print x
y = 10
z = x + y
print z

Note: From now on, there may be multi-line statements of code for you to type in. Simply hit Enter after each line.

Variable Types 

Variables are core to most of the programming you’ll do in your life. You’ll become intimately familiar with them as you work your way through this tutorial!
You encountered variable types a little earlier in this tutorial, but we didn’t cover them in any detail. Each variable type stores a different kind of value.
So far, you’ve only dealt with two basic types in Python: integers and strings. You’ll also encounter boolean types which you can use to store True and False values.

Here’s a bit of detail on each of these variable types:

Integers

An integer is a whole number. Integers range from -2147483648 to 2147483647 on 32-bit machines, and from -9223372036854775808 to 9223372036854775807 on 64-bit machines.

You create an integer variable by simply typing the number without any quotation marks, as shown below:
a = 5

Strings

A string is a sequence of characters; you can use a string to represent anything from the text on a screen to entire web requests.
You create a string variable by surrounding the sequence of characters with quotation marks as shown below:
b = "Codeschoolspy.com"

Booleans

Booleans represent either True or False values.
You create a boolean variable by typing a capitalized True or False without quotation marks as shown below:
are_you_boy = True

There aren’t any quotation marks around the variable; if you did surround True with quotation marks, you’d be declaring it as a string variable instead!

Adding Strings and Integers

Python makes it really easy to hook strings together, also known as concatenation. You can convert values from integer types to string types using the str() command. Likewise, you can convert a value from a string to an integer by using int().

Enter the following commands into your interpreter:
"1" + "1"
1 + 1
1 + int("1")

Here’s what’s going on in your code above:

1. The first statement concatenates the two strings; the quotation marks ensure the numbers are treated as strings. The result is simply "11".
2. The second statement adds the two numbers together as integers with 2 as the result.
3. The final statement adds an integer to a string that has been converted to an integer, so it also returns 

If Statements

If statements check whether a conditional is true and if so, they execute a block of code. Conditionals are usually in the form of value – operator – value and usually compare two values.
For example, you could evaluate whether a variable is equal to zero with the expression x == 0, where == is the equality operator.
Here are some common comparisons in Python:
a == b: #Checks if a and b are equal
a != b: #Checks if a and b are not equal
a > b:  #Checks if a is greater than b
a < b:  #Checks if a is less than b
a >= b: #Checks if a is greater than or equal to b
a <= b: #Checks if a is less than or equal to b

If statements take the following form:
if conditional:
    do_statements_here

Note: how the do_statements_here line is indented. This is how you declare code blocks in Python. It's imperative that every line in a code block is indented by the same number of tabs or spaces as all other lines in the code block; this is a rule enforced by the language itself. In other words, don't mix tabs and spaces!

For Loops

For loops in Python loop through items in a list and set a variable to the current item in the list. A list is a collection of just about anything!

Type in the following code, indenting as shown below:
for x in range(10):
    print x

In this example, range(10) generates a list of numbers from 0 to 9. The for loop, in turn, sets x to each number in the range.

Just like an if statement, a for loop executes everything indented beneath it. In the above example, the indented code block only contains a single statement.

Since print is called ten times, once for each item in the list, the program prints the numbers from 0 to 9.

Functions

Functions are reusable blocks of code that perform specific tasks. For example, you could write a function to add two numbers together or print out a string.

You define and call a function as shown in the example below:

def hello():
    print "Hi"
for x in range(3):
    hello()

The indented code defines the statements which run when you call the function. Since print "Hi" is the only indented line under the function declaration, it's the only line that executes when the function is called — not the other lines below.

You call a function by typing the function name followed by open and closed parentheses. As shown earlier, hello() calls the function you declared above.

Functions are a little like walled gardens: they can't see anything outside of their own little area. This is known as variable scope. If you want a function to work with any outside data, you'll need to pass that data to the function.

This is accomplished using arguments — and no, functions don't argue with each other! :]

An argument is a variable that you pass to a function; the function can then access the value of that variable inside its walls.

You declare a function with arguments as follows:
def add(a, b):
    print a + b

The above function defines two arguments, a and b, separated by commas between the parentheses. When you call the function, the interpreter sets the value of a and b to the value of the variables you pass in.

Take a look at the example below:
def add(a, b):
    print a + b
add(1,2) # prints 3!

In the example above, the statement add(1,2) sets a and b to 1 and 2, respectively. The function then prints out the sum of the two numbers you passed in.

The above example prints out the result of the calculation — but what if you wanted to do something with the result of that calculation? What if you wanted to add other values to the result of the function?

To do this, you'll need to add a return statement to your function.

Consider the code below:
def add(a,b):
    return a + b
print add(2, 2) + 1

In the above example, your function adds the two numbers as before, but the return statement then passes the sum of the two numbers back to the calling statement.

That means the print statement above takes the value returned by add(2,2) and adds it to 1, which will give the final value of 5.

While Loops

While loops are similar to loops. For loops continue until you reach the end of a list, but while loops will repeat indefinitely until a given condition evaluates to False.

The general structure of a while loop is shown below:
while (conditional):
    run_statement

Generally, the conditional variable in a while loop is updated as the loop runs.

To see this in action, type the following into your interpreter:
x = 0
while x < 10:
    print x
    x += 1

This behaves the same as the for loop example above but uses a while loop instead.

Here's what the code above does:
1. Sets x = 0
2. Checks if x < 10
3. If x < 10 evaluates to True, then execute the block below. If it evaluates to False then exit the loop
4. Print x
5. Increment x by 1
6. One thing to watch out for while loops are creating an infinite loop.

To see how an infinite loop functions, type the following code into your interpreter:
x = 0
while True:
    print x
    x += 1

Get User Input

One cool thing about Python is that it's very easy to receive input in the form of text from the user. Input is any external data provided to a program, such as text or other directions which affect the behavior of a program.

Type the following code into your Python interpreter:
name = raw_input("What is your name? ")
print "Hello, " + name

The above code first asks the user for input; once the user types in their answer to the question the program assigns it to the variable name. Once that's done, the program then concatenates the string "Hello, " with the contents of the variable name.

The raw_input() function is built right into Python; it does the work of printing out the provided string to the console, captures the text input from the user, then returns the captured text as the return value of the function.

You can make a simple calculator using the above technique of capturing user input.

Type the following code into your interpreter:
a = raw_input("Number 1: ")
b = raw_input("Number 2: ")
print int(a) + int(b)

First, you capture two values input by the user and assign them to the a and b variables respectively. Then you convert the input values into integers and add them together.

Why the conversion step? Simply because raw_input() interprets all input as strings, and you want to add the integer values together.

If you didn't convert the strings to integers, what do you think would happen? That's right — the program would print out the concatenation of the two strings, which is not what you want!

Imports

Before you dig into imports, it's worthwhile introducing the Python module.

A module is a group of Python functions that you can reuse in your programs. Importing a module is the equivalent of taking all the code from the module and putting it right into your program so you can use all of the functions whenever you want, but without cutting and pasting a single line of code!

There are lots of modules available in the Python community, but right now you'll just touch on the random module.

To import a module, type, the following into your interpreter:
import random

Once the random module has been imported, you can use it like this:
print random.randint(0, 100)

This prints out a random integer between 0 and 100; pretty straightforward stuff.

You've gained a lot of knowledge by this point, enough to put together a small program that exercises everything you've learned so far!


Note: Want to know more detail?
           Stay tuned with us soon we will publish a complete tutorial.