Python is a widely used general-purpose, high-level programming language. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C++ or Java. The language provides constructs intended to enable clear programs on both a small and large scale.

Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.

## Step 1: Expressions and Operators

An expression is a combination of values, variables, and operators. A value all by itself is considered an expression, and so is a variable.

print(10 + 2) # Addition = 12 print(10 + 2 - 6) # Addition and Subtraction = 6 print(10 * 2) # Multiplication = 20 print(10 * 2 / 2) # Multiplication and Division = 10 print(10 * 2 + 6) # Multiplication and Addition = 26 print(10 * (2 + 6))# Multiplication evaluates after Addition = 80

Operators are special symbols that represent computations like addition and multiplication. The values the operator is applied to are called operands.

Here is a list of Operators:

------------------------------------------------------------------------------- | Operator | | What it does | Example | ------------------------------------------------------------------------------- | + | | Numerical Addition | print(10 + 2) | | - | | Numerical Subtraction | print(10 - 2) | | * | | Multiply By | print(10 * 2) | | / | | Divided by | print(10 / 2) | ------------------------------------------------------------------------------

## Step 2: Statements and Control Flow

### Loops

In Python, there are two kinds of loops, 'for' loops and 'while' loops.

**For Loops**

A **for** loop iterates over elements of a sequence (tuple or list). A variable is created to represent the object in the sequence. For example,

a = [100,200,300] for i in x: print(i)

This will output:

100 200 300

The **for **loop loops over each of the elements of a list or iterator, assigning the current element to the variable name given. In the example above, each of the elements in ** x** is assigned to

**.**

*i*A built-in function called range exists to make creating sequential lists such as the one above easier. The loop above is equivalent to:

l = range(100,301,100) for i in l: print(i)

The next example uses a negative step (the third argument for the built-in range function):

for i in range(5,0,-1): print(i)

This will output:

5 4 3 2 1

The negative step can be -2:

for i in range(10,0,-2): print(i)

This will output:

10 8 6 4 2

**While Loops**

A while loop repeats a sequence of statements until some condition becomes false. For example:

x = 5 while x > 0: print(x) x = x - 1

This will output:

5 4 3 2 1

Python's while loops can also have an 'else' clause, which is a block of statements that is executed (once) when the while statement evaluates to false. The break statement inside the while loop will not direct the program flow to the else clause. For example:

x = 5 y = x while y > 0: print(y) y = y - 1 else: print(x)

This will output:

5 4 3 2 1 5

**Breaking and continuing**

Python includes statements to exit a loop (either a for loop or a while loop) prematurely. To exit a loop, use the break statement:

x = 5 while x > 0: print(x) break x-=1 print(x)

This will output

5

The statement to begin the next iteration of the loop without waiting for the end of the current loop is 'continue'.

l = [5,6,7] for x in l: continue print(x)

This will not produce any output, but this will:

l = [5,6,7] for x in l: print(x)

**If Statements**

Here is a warm-up exercise - a short program to compute the absolute value of a number:

n = raw_input("Type a Number: ") #Choose a number. n = int(n) #Defines n as the integer you chose. if n < 0: print("The absolute value of "+n+" is "+-n) else: print("The absolute value of "+n+" is "+n)

Here is the output from the two times when I ran this program:

Type a Number: -34 The absolute value of -34 is 34 Type a Number: 1 The absolute value of 1 is 1

First it prompts the user for a number with the statement "n = raw_input("Integer? ")". Next it reads the line "if n < 0:". If n is less than zero Python runs the line "print "The absolute value of",n,"is",-n". Otherwise python runs the line "print "The absolute value of",n,"is",n".

More formally, Python looks at whether the expression n < 0 is true or false. An if statement is followed by an indented block of statements that are run when the expression is true. After the if statement is an optional else statement and another indented block of statements. This 2nd block of statements is run if the expression is false.

Basic Syntax of an **if** statement:

if this == that: do this... else: do that...

## 7 Discussions

1 year ago

very good,i study your tutorial and i liked ;) Thanks brother!

1 year ago

thanks bro...really good.

3 years ago

Thank you for the

greatinstructable.Reply 3 years ago

Thanks

3 years ago on Introduction

i need assistance please.i transferred my site from wamp server to online server one is not displaying the other is showing this error (

Unable to open WampServer's config file, please change path in index.php file).

can someone help me

4 years ago

great instructable

Reply 4 years ago

Thanks