Introduction

Python is a general-purpose programming language that can be used for a wide variety of software development besides web development.
Python is easy-to-read and powerful, it includes high-level data structures, dynamic typing, dynamic binding, and many more features that make it as useful for complex application development as it is for scripting that connects components together.

Python can be used for things like:

  • Back-end(or server-side) web and mobile development
  • Desktop app and software development
  • Processing big data and performing mathematical computations
  • Writing scripts(or creating instructions that tell a computer system to "do" something)

Things You Need To Know

Below is a list of some of the prequisites for you:

  1. The difference between Front-end and Back-end development
  2. Understand what you can with Python
  3. Install Python (on your PC Mac)
  4. Python 2 vs. Python 3 - Know the difference
  5. You can be a Python developer without knowing "everything" about Python

Hello World

Python has a straight forwad syntax, it encourages a programmer to program without a boilerplate(prepared) code. The simplest directive in Python is the "print" command - it simply outputs a line of code(and also includes a newline).

To get started with writing Python, just open a text editor of your choice and write this code:

  print("Hello World")

Variables

Variables are symbolic names for values in your application,they are reserved memory locations for storing values.
In simple terms, variables are names given to objects, so that it becomes easy to refer to a value(in otherwards a variable points to an object since "Every variable in Python is an object")

Unlike other programming languages, Python has no for declaring a variable. A variable is created the moment you first assign a value to it.
Variables do not need to be declared with any particular type, and can even change the type after they have been set.

Example:

  x = 5
  y = "John"
  print(x)
  print(y)


  x = 5 #x is of type int(integer)
  y = "John" #y is of type str (string)

Variable Names

A variable can a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for Python variables:

  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive (age, Age and AGE are three different variables)
  • A variable name must have a meaning in relation to its value

Legal variable names:

  myvar = "John"
  my_var = "John"
  _my_var = "John"
  myVar = "John"
  MYVAR = "John"
  myvar2 = "John"

Illegal variable names:

  2myvar = "John"
  my-var = "John"
  my var = "John

Assign Value to Multiple Variables

Python allows you to assign values to multiple variables in one line:

  x, y, z = "Orange", "Banana", "Cherry"
  print(x)
  print(y)
  print(z)

And you can assign the same value to multiple variables in one line:

  x = y = z = "Orange"
  print(x)
  print(y)
  print(z)

Global Variables

Variables that are created outside of a function are known as Global Variables

Global variables can be used by everyone, both inside of functions and outside.

Create a variable outside of a function, and use it inside the function:

  x = "awesome"

  def myfunc():
     print("Python is " + x)

  myfunc()

Create a variable inside a function, with the same name as the global variable:

  x = "awesome"

  def myfunc():
     x = "fantastic"
     print("Python is " + x)

  myfunc()

  print("Python is " + x)

The global keyword

Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function.

To create a global variable inside a function, you can use the global keyword.
If you use the global keyword, the variable belongs to the global scope:

  def myfunc():
     global x
     x = "fantastic"

  myfunc()

  print("Python is " + x)

Also, use the global keyword if you want to change a global variable inside a function, refer to the variable by using the global keyword:

  x = "awesome"

  def myfunc():
     global x
     x = "fantastic"

  myfunc()

  print("Python is " + x)

Data Types

A data type a classification that specifies which type of value a variable has.

Python has the following data types built-in by default, in these categories:

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

  Example   Data Type
  x = "Hello World"   str
  x = 20   int
  x = 0.5   float
  ij   complex
  [ "apple", "banana", "cherry" ]   list
  ( "apple", "banana", "cherry" )   tuple
  x = range(6)   range
  x = {"name" : "John", "age" : 36}   dict
  x = {"apple", "banana", "cherry"}   set
  x = frozenset({"apple", "banana", "cherry"})   forzenset
  x = True   bool
  x = b"Hello"   bytes
  x = bytearray(5)   bytearray
  x = memoryview(bytes(5))   memoryview

If ... Else Statement

Use the if statement to execute a statement if a logical condition is true. Use the optional else clause to execute a statement if the condition is false. An if statement looks as follows:

  if (condition):
     statement_1
  else:
     statement_2

Condition can be any expression that evaluates to true or false. See Boolean for an explanation of what evaluates to true and false. If condition evaluates to true, statement_1 is executed; otherwise, statement_2 is executed. statement_1 and statement_2 can be any statement, including further nested if statements.

Elif statement

Use the elif condition is used to include multiple conditional expressions between if and else as follows:

  if [boolean expression_1]:
     statement_1
  elif [boolean expresion_2]:
     statement_2
  elif [boolean expresion_3]:
     statement_3
  elif [boolean expresion_4]:
     statement_4
  else:
     statement_5

Loops

A loop statement allows us to execute a statement or group of statements multiple times.

Python has two primitve loops commands.

  • while loops
  • for loops

While loop

A while loop statement repeatedly executes a target statement as long as a given condition is true.

The syntax of a while loop in Python programming language is:

  while [expression]:
     statement(s)

Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true.
When the condition becomes false, program control passes to the line immediately following the loop

  count = 0
  while (count < 9):
     print 'The count is:', count
     count = count + 1

  print "Good bye!"

For loop

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc

Syntax:

  for iterating_var in sequence:
     statements(s)

If a sequence contains an expression list, it is evaluated first. Then, the first item in the sequence is assigned to the iterating variable iterating_var. Next, the statements block is executed. Each item in the list is assigned to iterating_var, and the statement(s) block is executed until the entire sequence is exhausted.

  for letter in 'Python': # First Example
     print 'Current Letter :', letter

  fruits = ['banana', 'apple', 'mango'] # Second Example
  for fruit in fruits:
     print 'Current fruit :', fruit

  print "Good bye!"

Functions

A function is a block of organized, reusable code that is used to perform a single, related action only when its called.

You can pass data, known as parameters (inside the parentheses), into a function when its being defined.
A function can return data as a result

In Python a function is defined using the def keyword followed by the function name and parentheses "()":

  def function_name( parameters ):
     function_suite

  def my_function(str):
     print("Hello " + str)

To call a function, use the function name followed by parenthesis:

  def my_function():
     print("Hello from a function")

  my_function()

Information can be passed into functions as arguments during the time when they are being called.

Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.

The following example has a function with one argument (fname). When the function is called, we pass along a first name, which is used inside the function to print the full name:

  def my_function(fname):
     print(fname + " Refsnes")

  my_function("Emil")
  my_function("Tobias")
  my_function("Linus")

Reference

All the documentation in this page is combined from different websites on Google