Python – If Statement

Most of the time, every programmer feels the need to compare on or two values of variables in your program. Same as most of the programming languages, Python also has its syntax for the If Statement. In this article, I created a program that uses an if statement after the variables x, and y are declared. I assigned values 10 and 5 to variables x and y respectively.

The program is below:

if1

Your output should be the same as here:

if2

Thank you for reading this post.

Don’t forget to leave a comment.

Advertisements

Python – Loops(While Loop)

This article is a continuation of this last post:
https://arjunaraneta.wordpress.com/2014/08/26/python-loopsfor-loop/

The difference is I used a while in this program. The syntax of the for and while loops and different in terms of the codes used but the concept of a block with a starting value, ending value, and a process of increase and decrease are the same.

 

Here is my code.

loop3

 

Your output should be the same as below:
loop2

 

Thank you for reading this post.

Python – Loops(For Loop)

Most of the time, students get confused of the usage of looping. I always say, “Imagine a block of code that prints 10 consecutive numbers. One way of doing it is by printing them one-by-one. That would be easy if printing 10 numbers is the case but if your program needs to print 1000 numbers then that would be a very long program just for printing. The answer to that problem is looping. If you use a loop, you just define a starting point, a condition of where you are going to stop, and the process if it is increasing or decreasing. A block of 1000 codes would just take three to four lines of codes.”

 

In this program we are going to use a loop called a for loop. I used a variable x to hold the value that will be printed then I declared the starting value and ending value which are inside the range.

Below is my program.

loop1

 

Here is my output:

loop2

 

Thank you for reading this post. 🙂

Python – Arithmetic Operations Basics

One of the most used syntax in a language are the arithmetic operations. Operations like addition, subtraction, multiplication, division, and modulus are the basics. I created a program that shows the syntax for them. I first declared two variables namely x and y.  Then I performed the arithmetic operations using each variables in the following lines.

The code is below:
math1

 

Click run then select run module.

The output should be same as below:

 

math2

 

Thank you for reading this post.

Python – Basic Input

In this article, I am going to show you how to get input from the user. In this particular post, I declared a variable which is called name. After getting the name, the output will be the string “Hello” then the name.

The codes are shown here:

input1

 

 

Click run then select run module.

The output should be:

input2

 

Thank you for reading this post.

Python – Variables

You can think of variables as an address in your memory that contains data that you’ll need and use. The syntax for using variables in python in very straightforward. If you are a beginner, you should begin here:
https://arjunaraneta.wordpress.com/2014/08/02/python-basic-output-print/

Here is a basic example of declaring a variable named x with a value of 10. After declaring and assigning a value, the program will output the value of x.

var1

 

The output should be the same as below:

var2

 

On the next example below, I declared two variables named x and y. I assigned a value for each of the variables. After assigning the values, I created another variable named z assigned the sum of x and y. The program will output the result after the addition.

 

var3

The output should be the same as below:

var4

 

 

Thank you for reading my blog. Don’t forget to leave a comment.

Python – Basic Output (print)

In this tutorial, I will be introducing you to a language called Python. We will be creating a simple program that outputs “Hello World” on the screen.

First, you should download python here: https://www.python.org/downloads/

 

Then, install the software. Installation is very easy because all you need to do is read and follow the directions.

 

After installation, create a new folder in My Documents named tutorials.

While inside the tutorials folder, create a new text document and rename it tut1.py.

Open the file with your favorite text editor (I love to use Notepad++).

Type the codes same as below:

 

py1

Next, open the program named IDLE(Python GUI).

We should have the same screen as below.

py2

 

Click File -> Open, locate the file you created named tut1.py.

The window below should appear.

py3

 

Click Run -> Run Module.

The program will run your program and the output should be the same as below.

py4