On to Python 3
On to Python 3...
¡°Hello, World¡±
? C #include
int main(int argc, char ** argv)
{
! printf("Hello, World!\n");
}
? Javapublic class Hello
{
!
!
!
!
}
public static void main(String argv[])
{
! System.out.println("Hello, World!");
}
in Python
? now
print("Hello, World!")
2
Printing an Array
void print_array(char* a[], int len)
{
! int i;
has to specify len,
! for (i = 0; i < len; i++)
and only for one type (char*)
! {
! ! printf("%s\n", a[i]);
! }
}
C
for element in list:
! print(element)
only indentations
no { ... } blocks!
or even simpler:
for ... in ... :
...
Python
no C-style for-loops!
for (i = 0; i < 10; i++)
print list
3
Reversing an Array
static int[] reverse_array(int a[])
{
! int [] temp = new int[ a.length ];
! for (int i = 0; i < len; i++)
! {
! ! temp [i] = a [a.length - i - 1];
! }
! return temp;
}
def rev(a):
def ...(...):
! if a == []:
...
! ! return []
! else:
! ! return rev(a[1:]) + [a[0]]
or even simpler:
a.reverse()
a without a[0]
Java
Python
no need to specify argument
and return types!
python will figure it out.
(dynamically typed)
singleton list
built-in list-processing function
4
Quick-sort
public void sort(int low, int high)
{
! if (low >= high) return;
! int p = partition(low, high);
! sort(low, p);
! sort(p + 1, high);
}
void swap(int i, int j)
{!
! int temp = a[i];
! a[i] = a[j];
! a[j] = temp;
}
int partition(int low, int high)
{
! int pivot = a[low];
! int i = low - 1;
! int j = high + 1;
! while (i < j)
! {
! ! i++; while (a[i] < pivot) i++;
! ! j--; while (a[j] > pivot) j--;
! ! if (i < j) swap(i, j);
! }
! return j;
}
def sort(a):
{x | x ¡Ê a, x < pivot}
! if a == []:
! ! return []
! else:
! ! pivot = a[0]! !
! ! left = [x for x in a if x < pivot ]
smaller
! ! right = [x for x in a[1:] if x >= pivot]
! ! return sort(left) + [pivot] + sort(right)
how about return [sort(left)] + [pivot] + [sort(right)]
Java
Python
semantic-gap!
got an error??
5
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- python 3 list to str
- python 3 on centos 7
- installing matplotlib on python 3 8 3
- install python 3 8 on centos
- install python 3 on windows
- install python 3 7 on centos 7
- install python 3 on windows 10
- installing numpy on python 3 8
- upgrade python 3 6 to 3 8
- switch to python 3 linux
- install python 3 7 on linux
- how to add python 3 9 to path