Python For C/Java Programmer

What is Python?

Characteristics

  • Interpreted language
  • Object-oriented
  • Cross-platform

What can Python do?

  • Web development (server-side)
  • Software development
  • Mathmatics (e.g. Ploting graphs)
  • System scripting

Compiler And Interpreter

The compiler translates a whole program, but interpreter translates a program statement by statement.

A compiler is faster than interpreter.

Before Getting Start With Python

Do not use ; to separate statements
Do not use {} for the body of if/while/for/a function, use “four spaces” or a tab instead
Do append a : after the condition of if/while/for or function signature

Indentation is also important.

The indentation rule may seem unusual at first glance to programmers accustomed to C-like languages, but it is a deliberate feature of Python, and it’s one of the main ways that Python almost forces programmers to produce uniform, regular, and readable code. It essentially means that you must line up your code vertically, in columns, ac- cording to its logical structure. The net effect is to make your code more consistent and readable (unlike much of the code written in C-like languages).

To put that more strongly, aligning your code according to its logical structure is a major part of making it readable, and thus reusable and maintainable, by yourself and others. In fact, even if you never use Python after reading this book, you should get into the habit of aligning your code for readability in any block-structured language. Python underscores the issue by making this a part of its syntax, but it’s an important thing to do in any programming language, and it has a huge impact on the usefulness of your code.

Variables Assignment In Python Vs C/Java

Types Python C/Java
Integers v = 9 int v = 9;
Floating point number v = 9.99 float v = 9.99;
String v = “Hello” String v = “Hello”;

Types Conversion In Python VS C/Java

Example 1

Python

1
2
3
4
5
a = 999
b = 3.1415
a = b

#Value of a is 3.1415

C/Java

1
2
3
4
5
int a = 999;
float b = 3.1415;
a = b;

//Value of a is 3

Example 2

Python

1
2
3
4
5
c = "hello world"
d = 777
c = d

#Value of c is 777

C/Java

1
2
3
4
5
String c = "hello world";
int d = 777;
c = d; //Error

//Not Valid

How Python Manages Memory for Variables

Python does not recycle these memory locations. Integer values are immutable.

Assigning a variable is more like putting a “sticky note” on a value and saying, “this is x”.
The memory for the old value will be “released” automatically (i.e., garbage collection).

Basic IO In Python

Output

1
2
3
4
5
print("yo")
lel = ("LeL")
print(lel)

print(lel, "= lel")

Input

1
2
3
4
a = input("Input a String")
b = eval(input("Input a number"))
c,d = eval(input("Input two numbers"))
e,f,g = eval(input("Input three numbers"))

Boolean Operation In Python VS C/Java

/ Python C/Java
Nothing None null
Boolean value True / False true / false
And, Or, Not and, or, not &&, ||, !

if else-if else In Python

if, elif, else

example:

1
2
3
4
a = 33
b = 200
if b > a:
print("b is greater than a")

for loop In Python vs C/Java

Python

1
2
for num in range(0,10): #0 ~ 9
print(num)

Loop from a array

1
2
3
4
niceguy = ["Vines", "GSam", "MSam", "Bon"]

for names in niceguy:
print(names)

Java

1
2
for(int num = 0; num < 10; num++)
//print num

Loop from a array

1
2
3
4
String[] niceguy = ["Vines", "GSam", "MSam", "Bon"]

for(String names:niceguy)
//print names

switch in Python

There is no switch in Python

Linear Data Structure In Python

4 Linear Data Structure will be introduced.

  • List - Similar to array
    • boys = ["Peter","Frank","Chris"]
  • Tuple - A List which can not be changed (immutable)
    • boys = ("Peter","Frank","Chris")
  • String
    • boys = "Peter"
  • Set - A list of non-mutable objects
    • boys = {"Peter","Frank","Chris"}

List Operations

Tuple Operations

Tuples are immutable (cannot append or delete elements)

String Operations

Slicing: <string>[<start>:<end>]

example: str[0:3], str[0:], str[-1:2]

Common Operations For Sequences

Set Operations

Advance Data Structure

Dictionary

A dictionary is a mutable, associative data structure of variable length.

  • The key can be of any immutable type.
  • The values can be of any type and are unordered.
1
2
3
4
5
Dict-example = {key1: value1, key2: value2}

More-example = {"brand": "Ford", "model": "Mustang", "year":1964}

Dict-inside-a-Dict = {"a":1, "b":{"aa":11, "bb":22 } }

Dictionary Operations

Dictionary Methods

Python provides a number of other methods that iterate through the elements in a dictionary.

  • items() - Return all the key-values pairs as a list of tuples
  • keys() - Return all the keys as a list
  • values() - Return all the values as a list

For Loop And Dictionary

1
2
3
whohasfood = {"Frank": "Pizza", "Vines": "Banana", "Sally": "IceCream"}
for key,value in whohasfood.items():
print("key: ", key, "value: ", value)

Function In Python

Declaring a Function

Without return

1
2
3
4
5
6
def functionName(): #Return Type is not needed
#ToDo


# Call your function
functionName()

With return

1
2
3
4
5
6
def average(v1,v2): #Return Type is not needed
return (v1+v2)/2

# Call your function
avg = average(5,9)
print(avg)

Build-in Function In Python

Modules In Python

You should know how to use a function from different module.

C++

1
2
#include <iostream>
#include "file.h"

Java

1
import abc

Python

1
2
import numpy as np
import random as rd

Continue Your Journey With Python