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.
You will get something like this:
Try to type a simple calculation formula here.
Then you have a simple CLI calculator! To use it as a calculator, _
refers to the last output, just like the ANS
in calculators.
We can also set a variable and let it remember.
And we can just type a
and we can get its value
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:
And press i
to Enter INSERT mode to type the code.
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:
Complex Data Types
In Python, we have List, Set, Dictionary, 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.
To work with dictionaries, we can use the key to get the value.
Loop and Condition
We want to print out all of the items in a list, we can simply use a for-each loop:
Or something similar as Java:
Or If we want to loop a dictionary:
if
and else
are very similar to that in Java:
And Python can make checking easier:
Functions
Defining a function in Python is also similar to that in Java.
Adding return value. And if __name__ == '__main__':
means the main method.
Try an Example
👍 Goal: Rewrite this Java code into Python code(script)
Note that:
Boolean value in Python is
True
orFalse
Activity: Clash of Code
Last updated