Skip to content

Instantly share code, notes, and snippets.

View lfalanga's full-sized avatar
🏠
Working from home.

Leandro Falanga lfalanga

🏠
Working from home.
  • Buenos Aires, Argentina.
View GitHub Profile
from random import randint
board = []
for x in range(5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print " ".join(row)
def print_board(board):
for row in board:
print " ".join(row)
def random_row(board):
return randint(0, len(board) - 1)
def random_col(board):
return randint(0, len(board[0]) - 1)
# Example 1: Python3 program for pop() method
list1 = [ 1, 2, 3, 4, 5, 6 ]
# Pops and removes the last element from the list
print(list1.pop())
# Print list after removing last element
print("New List after pop : ", list1, "\n")
list2 = [1, 2, 3, ('cat', 'bat'), 4]
# Example 1: Simple while loop
count = 0
if count < 5:
print "Hello, I am an if statement and count is", count
while count <= 9:
print "Hello, I am a while and count is", count
count += 1
# Example 2: Another simple while loop
# Example 1: Simple While / Else loop
import random
print "Lucky Numbers! 3 numbers will be generated."
print "If one of them is a '5', you lose!"
count = 0
while count < 3:
num = random.randint(1, 6)
print num
# Example 1: Verifies that the given number is an integer (not just the type). Works for floats too.
def is_int(x):
if x - round(x) == 0:
return True
else:
return False
# Example 2: Summarize all digits for a given number (non negative)
def digit_sum(n):
result = 0
# Example 1: Anonymous function
"""
NOTE:
One of the more powerful aspects of Python is that it allows for a style of
programming called functional programming, which means that you’re allowed to
pass functions around just as if they were variables or values. Sometimes we
take this for granted, but not all languages allow this!
Lambdas are useful when you need a quick function to do some work for you.
For example:
# Example 1: Basic bitwise operations
print 5 >> 4 # Right Shift
# => 0
print 5 << 1 # Left Shift
# => 10
print 8 & 5 # Bitwise AND
# => 0
print 9 | 4 # Bitwise OR
# => 13
print 12 ^ 42 # Bitwise XOR
# Example 1: Displaying a simple Hello, World! label
from tkinter import *
Label(text='Hello, World!').pack()
mainloop()
# Example 2: Displaying an entry (field) in Tkinter window
from tkinter import *
root = Tk()
e = Entry(root)
e.pack()
from tkinter import *
root = Tk()
e = Entry(root)
e.pack()
e.focus_set()
#########
a = 5
b = "2"
c = a + int(b)