Transfer to Python

Free Calculator

Now we will get our hands dirty to play with Python in the Command-Line. Python can be run in REPL(Read–eval–print loop). It is a simple way to code in Python. Type python without any parameters in the shell.
python
You will get something like this:
Python 3.9.7 | packaged by conda-forge | (default, Sep 29 2021, 19:24:02)
[Clang 11.1.0 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Try to type a simple calculation formula here.
>>> 26374 + 88140
114514
Then you have a simple CLI calculator! To use it as a calculator, _ refers to the last output, just like the ANS in calculators.
>>> _ + 1805296
1919810
We can also set a variable and let it remember.
>>> a = -1
And we can just type a and we can get its value
>>> a
-1

Writing Python Hello World.

Now using Ctrl + D to quit REPL. And let's try to write some Python code by Vim. First, cd to anywhere you like. Then:
vim hello.py
And press i to Enter INSERT mode to type the code.
print("Hello, World")
Python allows you not to type ; at the end of a line. Press the Esc key and type :x to exit Vim. Then run the code by:
python hello.py

Complex Data Types

In Python, we have List, Set, Dictionary, Tuple.
>>> a = {1, 2, 3}
>>> type(a)
<class 'set'>
>>> b = [1, 2, 3]
>>> type(b)
<class 'list'>
>>> c = {1: 'a', 2: 'b'}
>>> type(c)
<class 'dict'>
>>> d = (1, 2, 3)
>>> type(d)
<class 'tuple'>
List
Tuple
Set
Dictionary
List is a non-homogeneous data structure which stores the elements in single row and multiple rows and columns
Tuple is also a non-homogeneous data structure which stores single row and multiple rows and columns
Set data structure is also non-homogeneous data structure but stores in single row
Dictionary is also a non-homogeneous data structure which stores key value pairs
List can be represented by [ ]
Tuple can be represented by
( )
Set can be represented by { }
Dictionary can be represented by { }
List allows duplicate elements
Tuple allows duplicate elements
Set will not allow duplicate elements
Set will not allow duplicate elements and dictionary doesn’t allow duplicate keys.
List can use nested among all
Tuple can use nested among all
Set can use nested among all
Dictionary can use nested among all
Example: [1, 2, 3, 4, 5]
Example: (1, 2, 3, 4, 5)
Example: {1, 2, 3, 4, 5}
Example: {1, 2, 3, 4, 5}
List can be created using list() function
Tuple can be created using tuple() function.
Set can be created using set() function
Dictionary can be created using dict() function.
List is mutable i.e we can make any changes in list.
Tuple is immutable i.e we can not make any changes in tuple
Set is mutable i.e we can make any changes in set. But elements are not duplicated.
Dictionary is mutable. But Keys are not duplicated.
List is ordered
Tuple is ordered
Set is unordered
Dictionary is ordered
Creating an empty list
l=[]
Creating an empty Tuple
t=()
Creating a set
a=set()
b=set(a)
Creating an empty dictionary
d={}
To access a list item by index, the syntax is the same as Java.
arr = [1, 2, 3, 4, 5]
print(arr[3]) # This will output 4
To work with dictionaries, we can use the key to get the value.
dict = {"Alice": 12, "Bob": 13, "Charlie": 14}
print(dict["Alice"]) # This will output 12

Loop and Condition

We want to print out all of the items in a list, we can simply use a for-each loop:
arr = [1, 2, 3, 4, 5]
for a in arr:
print(a)
Or something similar as Java:
arr = [1, 2, 3, 4, 5]
for i in range(len(arr)):
print(arr[i])
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
Or If we want to loop a dictionary:
dict = {"Alice": 12, "Bob": 13, "Charlie": 14}
for key in dict:
print(key, dict[key])
if and else are very similar to that in Java:
num = 10
if num >= 10:
print("num >= 10")
else:
print("num < 10")
int num = 10;
if (num >= 10) {
System.out.println("num >= 10");
} else {
System.out.println("num < 10");
}
And Python can make checking easier:
primes = [2, 3, 5, 7, 11, 13, 17, 19]
if 10 in primes:
print("10 is prime")
else:
print("10 is composite")

Functions

Defining a function in Python is also similar to that in Java.
def say_hello():
print("Hello")
public static void sayHello() {
System.out.println("Hello");
}
Adding return value. And if __name__ == '__main__': means the main method.
import math
def quadratic(a, b, c):
delta = b * b - 4 * a * c
if delta >=0:
root_A = ( -b - math.sqrt(delta) ) / 2 * a
root_B = ( -b + math.sqrt(delta) ) / 2 * a
return root_A, root_B
else:
return None
if __name__ == '__main__':
roots = quadratic(2, 3, 1)
print(roots)

Try an Example

👍 Goal: Rewrite this Java code into Python code(script)
class HelloWorld {
static boolean isPrime(int num) {
if (num == 0) return false;
if (num == 1) return false;
boolean flag = true;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
flag = false;
break;
}
}
return flag;
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
if (isPrime(i)) System.out.println(i);
}
}
}
Note that:
  • Boolean value in Python is True or False

Activity: Clash of Code