Moving to Python From C or Java [1]
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 ofif
/while
/for
/a function, use “four spaces” or a tab instead
Do append a:
after the condition ofif
/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 | a = 999 |
C/Java
1 | int a = 999; |
Example 2
Python
1 | c = "hello world" |
C/Java
1 | String c = "hello world"; |
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 | print("yo") |
Input
1 | a = input("Input a String") |
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 | a = 33 |
for loop In Python vs C/Java
Python
1 | for num in range(0,10): #0 ~ 9 |
Loop from a array
1 | niceguy = ["Vines", "GSam", "MSam", "Bon"] |
Java
1 | for(int num = 0; num < 10; num++) |
Loop from a array
1 | String[] niceguy = ["Vines", "GSam", "MSam", "Bon"] |
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 | Dict-example = {key1: value1, key2: value2} |
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 tupleskeys()
- Return all the keys as a listvalues()
- Return all the values as a list
For Loop And Dictionary
1 | whohasfood = {"Frank": "Pizza", "Vines": "Banana", "Sally": "IceCream"} |
Function In Python
Declaring a Function
Without return
1 | def functionName(): #Return Type is not needed |
With return
1 | def average(v1,v2): #Return Type is not needed |
Build-in Function In Python
Modules In Python
You should know how to use a function from different module.
C++
1 |
Java
1 | import abc |
Python
1 | import numpy as np |