I'm pretty brand new to Python and started with Python 2.7.10 for a start but I started thinking of 'updating' Python for some random reason. Plus there are many programers who 'stuck' with Python 2.7 for many reasons like in Python 3 you'd have to put parenthesis in a print statement (below).

Python 2

print "Hi!"

Python 3

print("Hi")
#Super Wierd!

This really freaks people out, why can't they just be the same script. It's a lot easier. Even if you do put parenthesis in Python 2 it won't make a freak-you-out excuse like

Python 3.3.5 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> print "Hi"
invaild syntax : unexpected indent

That's why 'most' people stick to Python 2.7 and there are various changes for Python (e.g Tkinter is now tkinter) and most modules had renaming which will (most super-super-absolutely-tremendously) confuse them. Some refuse to this though. I tried to look at many codes that are the same, but in a different Python language (below).

Bubble Blaster 1.8.py - Python 2

from Tkinter import *
HEIGHT = 626
WIDTH = 1238
window = Tk()   
window.title('Bubble Blaster 1.8 - By Lucas - AiroTM - Contact Us On Airo ;)')
c = Canvas(window, width=WIDTH, height=HEIGHT, bg='darkblue') 
c.pack()
ship_id = c.create_polygon(5, 5, 5, 25, 30, 15, fill='red')
ship_id2 = c.create_oval(0, 0, 30, 30, outline='white')
SHIP_R = 16
MID_X = WIDTH / 2
MID_Y = HEIGHT / 2
c.move(ship_id, MID_X, MID_Y)
c.move(ship_id2, MID_X, MID_Y)
SHIP_SPD = 12
def move_ship(event):
    if event.keysym == 'Up':
        c.move(ship_id, 0, -SHIP_SPD)
        c.move(ship_id2, 0, -SHIP_SPD)
    elif event.keysym == 'Down':
        c.move(ship_id, 0, SHIP_SPD)
        c.move(ship_id2, 0, SHIP_SPD)
    elif event.keysym == 'Left':
        c.move(ship_id, -SHIP_SPD, 0)
        c.move(ship_id2, -SHIP_SPD, 0)
    elif event.keysym == 'Right':
        c.move(ship_id, SHIP_SPD, 0)
        c.move(ship_id2, SHIP_SPD, 0)
c.bind_all('<Key>', move_ship)
from random import randint
bub_id = list()
bub_r = list()
bub_speed = list()
MIN_BUB_R = 10
MAX_BUB_R = 30
MAX_BUB_SPD = 20
sup_id = list()
sup_r = list()
sup_speed = list()
MIN_SUP_R = 10
MAX_SUP_R = 30
MAX_SUP_SPD = 35
GAP = 100
def create_bubble():
    x = WIDTH + GAP
    y = randint(0, HEIGHT)
    r = randint(MIN_BUB_R, MAX_BUB_R)
    id1 = c.create_oval(x - r, y - r, x + r, y + r, fill='lightblue')
    bub_id.append(id1)
    bub_r.append(r)
    bub_speed.append(randint(1, MAX_BUB_SPD))
    def create_sup():
    x = WIDTH + GAP
    y = randint(0, HEIGHT)
    r = randint(MIN_BUB_R, MAX_BUB_R)
    id1 = c.create_oval(x - r, y - r, x + r, y + r, fill='red')
    bub_id.append(id1)
    bub_r.append(r)
    bub_speed.append(randint(1, MAX_BUB_SPD))
def move_bubbles():
    for i in range(len(bub_id)):
        c.move(bub_id[i], -bub_speed[i], 0)
def move_sups():
    for i in range(len(sup_id)):
        c.move(sup_id[i], -sup_speed[i], 0)
from time import sleep, time
BUB_CHANCE = 10
#MAIN GAME LOOP
while True:
    if randint(1, BUB_CHANCE) == 1:
        create_bubble()
        create_sup()
    move_bubbles()
    move_sups()
    window.update()
    sleep(0.01)
def get_coords(id_num):
    pos = c.coords(id_num)
    x = (pos[0] + pos[2])/2
    y = (pos[1] + pos[3])/2
    return x, y
def del_bubble(i):
    del bub_r[i]
    del bub_speed[i]
    c.delete(bub_id[i])
    del bub_id[i]
def del_sup(i):
    del sup_r[i]
    del sup_speed[i]
    c.delete(sup_id[i])
    del sup_id[i]
def clean_up_bubs():
    for i in range(len(bub_id)-1, -1, -1):
        x, y = get_coords(bub_id[i])
        if x < -GAP:
            del_bubble(i)
def clean_up_sups():
    for i in range(len(sup_id)-1, -1, -1):
        x, y = get_coords(sup_id[i])
        if x < -GAP:
            del_sup(i)
#MAIN GAME LOOP
while True:
    if randint(1, BUB_CHANCE) == 1:
        create_bubble()
        create_sup()
    move_bubbles()
    move_sups
    clean_up_bubs()
    clean_up_sups
    window.update()
    sleep(0.01)
from math import sqrt
def distance(id1, id2):
    x1, y1 = get_coords(id1)
    x2, y2 = get_coords(id2)
    return sqrt((x2 - x1)**2 + (y2 - y1)**2)
def collision():
    points = 0
    for bub in range(len(bub_id)-1, -1, -1):
        if distance(ship_id2, bub_id[bub]) < (SHIP_R + bub_r[bub]):
            points += (bub_r[bub] + bub_speed[bub])
            del_bubble(bub)
    return points
def collision_sup():
    points = 0
    for sup in range(len(sup_id)-1, -1, -1):
        if distance(ship_id2, sup_id[sup]) < (SHIP_R + sup_r[sup]):
            points += (bub_r[sup] + bub_speed[sup])
            del_bubble(sup)
            points = points + 1
    return points
score = 0
#MAIN GAME LOOP
while True:
    if randint(1, BUB_CHANCE) == 1:
        create_bubble()
        create_sup()
    move_bubbles()
    move_subs()
    clean_up_bubs()
    clean_up_sups()
    score += collision()
    print(score)
    window.update()
    sleep(0.01)
c.create_text(50, 30, text='TIME', fill='white' )
c.create_text(150, 30, text='SCORE', fill='white' )
time_text = c.create_text(50, 50, fill='white' )
score_text = c.create_text(150, 50, fill='white')
def show_score(score):
    c.itemconfig(score_text, text=str(score))
def show_time(time_left):
    c.itemconfig(time_text, text=str(time_left))
from time import sleep, time
BUB_CHANCE = 10
TIME_LIMIT = 30
BONUS_SCORE = 1000
score = 0
bonus = 0
end = time() + TIME_LIMIT
#MAIN GAME LOOP
while time() < end:
    if randint(1, BUB_CHANCE) == 1:
        create_bubble()
        create_sup()
    move_bubbles()
    move_sups()
    clean_up_bubs()
    clean_up_sups()
    score += collision
    if (int(score / BONUS_SCORE)) > bonus:
        bonus += 1
        end += TIME_LIMIT
    show_score(score)
    show_time(int(end - time()))
    window.update()

c.create_text(MID_X, MID_Y, \
   text='GAME OVER!', fill='white', font=('Helvetica', 30))
c.create_text(MID_X, MID_Y + 30, \
   text='Score: '+ str(score), fill='white')
c.create_text(MID_X, MID_Y + 45, \
     text='Bonus time: '+ str(bonus*TIME_LIMIT), fill='white')

Python 3

from tkinter import *
  HEIGHT = 626
  WIDTH = 1238
   window = Tk()   
    window.title('Bubble Blaster 1.8 - By Lucas - AiroTM - Contact Us On Airo ;)')
    c = Canvas(window, width=WIDTH, height=HEIGHT, bg='darkblue') 
    c.pack()
    ship_id = c.create_polygon(5, 5, 5, 25, 30, 15, fill='red')
    ship_id2 = c.create_oval(0, 0, 30, 30, outline='white')
    SHIP_R = 16
    MID_X = WIDTH / 2
    MID_Y = HEIGHT / 2
    c.move(ship_id, MID_X, MID_Y)
    c.move(ship_id2, MID_X, MID_Y)
    SHIP_SPD = 12
    def move_ship(event):
        if event.keysym == 'Up':
            c.move(ship_id, 0, -SHIP_SPD)
            c.move(ship_id2, 0, -SHIP_SPD)
        elif event.keysym == 'Down':
            c.move(ship_id, 0, SHIP_SPD)
            c.move(ship_id2, 0, SHIP_SPD)
        elif event.keysym == 'Left':
            c.move(ship_id, -SHIP_SPD, 0)
            c.move(ship_id2, -SHIP_SPD, 0)
        elif event.keysym == 'Right':
            c.move(ship_id, SHIP_SPD, 0)
            c.move(ship_id2, SHIP_SPD, 0)
    c.bind_all('<Key>', move_ship)
    from random import randint
    bub_id = list()
    bub_r = list()
    bub_speed = list()
    MIN_BUB_R = 10
    MAX_BUB_R = 30
    MAX_BUB_SPD = 20
sup_id = list()
    sup_r = list()
    sup_speed = list()
    MIN_SUP_R = 10
    MAX_SUP_R = 30
    MAX_SUP_SPD = 35
    GAP = 100
def create_bubble():
        x = WIDTH + GAP
        y = randint(0, HEIGHT)
        r = randint(MIN_BUB_R, MAX_BUB_R)
        id1 = c.create_oval(x - r, y - r, x + r, y + r, fill='lightblue')
        bub_id.append(id1)
        bub_r.append(r)
    bub_speed.append(randint(1, MAX_BUB_SPD))
        def create_sup():
        x = WIDTH + GAP
        y = randint(0, HEIGHT)
        r = randint(MIN_BUB_R, MAX_BUB_R)
        id1 = c.create_oval(x - r, y - r, x + r, y + r, fill='red')
    bub_id.append(id1)
        bub_r.append(r)
        bub_speed.append(randint(1, MAX_BUB_SPD))
    def move_bubbles():
        for i in range(len(bub_id)):
            c.move(bub_id[i], -bub_speed[i], 0)
def move_sups():
        for i in range(len(sup_id)):
            c.move(sup_id[i], -sup_speed[i], 0)
    from time import sleep, time
    BUB_CHANCE = 10
#MAIN GAME LOOP
    while True:
        if randint(1, BUB_CHANCE) == 1:
            create_bubble()
            create_sup()
    move_bubbles()
        move_sups()
        window.update()
        sleep(0.01)
    def get_coords(id_num):
    pos = c.coords(id_num)
        x = (pos[0] + pos[2])/2
        y = (pos[1] + pos[3])/2
        return x, y
    def del_bubble(i):
        del bub_r[i]
    del bub_speed[i]
        c.delete(bub_id[i])
        del bub_id[i]
    def del_sup(i):
        del sup_r[i]
    del sup_speed[i]
        c.delete(sup_id[i])
        del sup_id[i]
    def clean_up_bubs():
        for i in range(len(bub_id)-1, -1, -1):
            x, y = get_coords(bub_id[i])
            if x < -GAP:
            del_bubble(i)
    def clean_up_sups():
        for i in range(len(sup_id)-1, -1, -1):
            x, y = get_coords(sup_id[i])
            if x < -GAP:
                del_sup(i)
    #MAIN GAME LOOP
    while True:
    if randint(1, BUB_CHANCE) == 1:
            create_bubble()
            create_sup()
        move_bubbles()
        move_sups
        clean_up_bubs()
        clean_up_sups
    window.update()
            sleep(0.01)
    from math import sqrt
    def distance(id1, id2):
        x1, y1 = get_coords(id1)
    x2, y2 = get_coords(id2)
        return sqrt((x2 - x1)**2 + (y2 - y1)**2)
    def collision():
        points = 0
        for bub in range(len(bub_id)-1, -1, -1):
            if distance(ship_id2, bub_id[bub]) < (SHIP_R + bub_r[bub]):
                points += (bub_r[bub] + bub_speed[bub])
                del_bubble(bub)
        return points
    def collision_sup():
        points = 0
        for sup in range(len(sup_id)-1, -1, -1):
        if distance(ship_id2, sup_id[sup]) < (SHIP_R + sup_r[sup]):
                points += (bub_r[sup] + bub_speed[sup])
                del_bubble(sup)
                points = points + 1
        return points
score = 0
    #MAIN GAME LOOP
    while True:
        if randint(1, BUB_CHANCE) == 1:
            create_bubble()
            create_sup()
        move_bubbles()
    move_subs()
        clean_up_bubs()
        clean_up_sups()
        score += collision()
        print(score)
        window.update()
    sleep(0.01)
    c.create_text(50, 30, text='TIME', fill='white' )
    c.create_text(150, 30, text='SCORE', fill='white' )
    time_text = c.create_text(50, 50, fill='white' )
    score_text = c.create_text(150, 50, fill='white')
    def show_score(score):
        c.itemconfig(score_text, text=str(score))
def show_time(time_left):
        c.itemconfig(time_text, text=str(time_left))
    from time import sleep, time
    BUB_CHANCE = 10
    TIME_LIMIT = 30
    BONUS_SCORE = 1000
    score = 0
    bonus = 0
end = time() + TIME_LIMIT
    #MAIN GAME LOOP
    while time() < end:
        if randint(1, BUB_CHANCE) == 1:
            create_bubble()
            create_sup()
        move_bubbles()
    move_sups()
        clean_up_bubs()
        clean_up_sups()
        score += collision
        if (int(score / BONUS_SCORE)) > bonus:
            bonus += 1
            end += TIME_LIMIT
        show_score(score)
    show_time(int(end - time()))
        window.update()

c.create_text(MID_X, MID_Y, \
       text='GAME OVER!', fill='white', font=('Helvetica', 30))
    c.create_text(MID_X, MID_Y + 30, \
       text='Score: '+ str(score), fill='white')
    c.create_text(MID_X, MID_Y + 45, \
         text='Bonus time: '+ str(bonus*TIME_LIMIT), fill='white')

There is also more when you make an image in Tkinter in Python 2.7 you don't get it unless it has a purpose like moving, some years later I also discovered it happened to text! However in Python 3 it is completlely the opposite.

Python 2

print "Hello World!"

Python 3

print("Hello World")

Anyway but should I update it?
Yes or no?
:1

Recommended Answers

All 6 Replies

If you are learning python now, learn python 3. It has been around for quite a while now, and many features have been added. If you learn python 2, you will not be fluent in modern python.

Some tiny details may seem weird, such as the parenthesis in print(), but believe me, the changes from python 2 to python 3 have been carefully studied by an amazing team of programmers, and it may take some time and some experience before you can understand the details of these changes. This is not your priority action as you learn the language.

What Gribouillis said. Python 2.x is a dead end so I see no reason not to focus on 3.x instead.

I kinda love Python, until I came across that 2.7 - 3.0 thing.
Started in 3.0 and learning. But most of the examples on the net are still in 2.7. This often caused error messages when using 3.0, I found it somewhat of a drawback. Never had those issues with C# 2.0 code. It compiles perfectly in a C# 6.0 environment. I guess the same is true for Java or C++ environments. Can anyone tell me the reasons why these compatibility issues still exist in Python?

I think these compatibility issues will exist forever. The solution
is to stop using python 2.x code. The main sources of backward incompatibility are

  • The print statement without ()
  • The literal strings which are byte strings in 2.x and unicode strings in 3.x
  • The new names of standard library modules.
  • Some details of integer arithmetics

Most of these incompatibilities can be solved by using the 2to3 tool to translate
old python 2 code into python 3.

The reasons for these incompatibilities lie both in python's initial design, with
its own flaws and its C-like culture from the early 90s and in the evolution of
programming and the internet, for example in the first decade of 2000, many
programs adapted to unicode.

commented: Surely helps. :) +15

Still it's very complex and who wants to put parenthesis in a piece of wonderful code!

Well e.g. print seems like a function to me, so I would put it in parenthesis.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.