Syntax, Variables, For Loops, and If/Then (Python Basics 1)

Basic Syntax

  • Semi-colons are unnecessary
  • Python is whitespace delimited
    • “tab in” or use spaces with for loops or if/then statements
    • “tab out” rather than using end
  • Use colons with compound statements such as for or if:
for i in range(10):
    print i

Declaring Variables

  • Python is dynamically typed
    • you do not need to declare your variables ahead of time

C

int a;
a = 7;

Python

a = 7

For Loops

for i in range(10):
    print i

For Loop Examples

Github:

https://github.com/ryanGT/python_basics_1

Files:

  • for_loop.py
  • for_loop_2.py
  • for_loop_3_two_lists.py

If/Then Statements

a = 7

if a == 0:
    print('a is zero')
elif a < 5:
    print('a is small')
else:
    print('a is large')

Github:

https://github.com/ryanGT/python_basics_1/blob/master/if_then_else.py