Introduction

What you should already know

This guide assumes you have the following basic background:

  • A general understanding of the Internet and the World Wide Web (WWW).
  • Some programming experience. If you are new to programming, try one of the tutorials linked on the main page about Python.
Python & Java

Python and Java are both widely used programming languages, but they have some fundamental differences. Python is an interpreted, high-level, general-purpose programming language. It emphasizes code readability with its notable use of significant indentation. Python is dynamically typed, meaning you don't need to declare a variable's type, unlike Java's static typing. This allows for a more flexible and faster development process.

In contrast to Java's compile-time system of classes, Python is often run directly by an interpreter. Python's object model is class-based, but its syntax is much more streamlined. Python also supports functions as first-class objects, meaning they can be assigned to variables, passed as arguments, or returned from other functions.

Python is a very free-form language compared to Java. You don't have to declare all variables, classes, and methods. You don't have to be concerned with whether methods are public, private, or protected, and you don't have to implement interfaces

Hello World

To get started with writing Python, open a text editor and write your first "Hello World" code:

def greet_me(your_name):
  print("Hello " + your_name)

greet_me("World")

Save the file as hello.py and run it from your terminal: python hello.py.

Variables

You use variables as symbolic names for values in your application. The names of variables, called identifiers, conform to certain rules.

A Python identifier must start with a letter (A-Z, a-z) or an underscore (_). Subsequent characters can also be digits (0-9). Because Python is case-sensitive, myVar is not the same as myvar.

Declaring Variables

In Python, you declare a variable by simply assigning it a value. For example, x = 42. Python is a dynamically typed language, so you don't need to specify the data type of the variable when you declare it.

Variable Scope

When you declare a variable outside of any function, it is called a global variable, because it is available to any other code in the current module. When you declare a variable within a function, it is called a local variable, because it is available only within that function.

# A global variable
message = "This is a global message"

def my_function():
  # A local variable
  local_message = "This is a local message"
  print(local_message)

print(message)
# This will cause an error: print(local_message)

To modify a global variable from within a function, you must use the global keyword.

Data Types

Python has several built-in data types:

  • Numeric Types: int, float, complex.
  • String Type: str.
  • Sequence Types: list, tuple, range.
  • Mapping Type: dict.
  • Set Types: set, frozenset.
  • Boolean Type: bool.
  • Binary Types: bytes, bytearray, memoryview.
Control Structures

if...else statement

Use the if statement to execute a block of code if a condition is true.

Use the optional else clause to execute another block of code if the condition is false.

if condition:
  statement_1
else:
  statement_2
      

condition can be any expression that evaluates to True or False. You may also use elif to check multiple conditions in sequence.

while statements

A while loop executes its statements as long as a specified condition evaluates to True.

i = 1
while i < 6:
  print(i)
  i += 1

      

If the condition becomes False, the loop stops executing.

Function Declarations

A function definition consists of the def keyword, followed by:

  • The name of the function.
  • A list of arguments to the function, enclosed in parentheses.
  • The Python statements that define the function, which are indented.

For example, the following code defines a simple function named square:

def square(number):
  return number * number
      

The function square takes one argument, number. The function consists of one statement that says to return the argument multiplied by itself.

The return statement specifies the value returned by the function.

Reference

All the documentation in this page is based on the [Official Python Documentation].