Skip to content

Instantly share code, notes, and snippets.

View studiawan's full-sized avatar

Hudan Studiawan studiawan

View GitHub Profile
@studiawan
studiawan / list-for.py
Last active March 11, 2021 04:50
Accessing python list
# declare and fill list
courses = ['j2ee', 'progjar', 'paal']
# iterate each element
for course in courses:
print(course)
# get index and value with enumerate
for index, value in enumerate(courses):
print(index, value)
@studiawan
studiawan / list.py
Last active March 11, 2021 04:55
Python list declaration, initialization, append and remove elements
# declaration without elements
courses = []
# declaration with elements
languages = ['python', 'java', 'c']
print(languages)
# fill the list
courses.append('progjar')
courses.append('j2ee')
@studiawan
studiawan / tuple.py
Last active March 11, 2021 04:55
Python tuple declaration, initialization, and how to access
# declaration without brackets
# elements are separated with comma
address = 'localhost', 80, 21
# declaration with brackets
server_address = ('localhost', 5000)
# access by index
print(server_address[0])
@studiawan
studiawan / dictionary.py
Last active March 11, 2021 04:57
Python dictionary
# declare dictionary
banner = {}
# fill dictionary
banner['os'] = 'Ubuntu Server 13.04'
banner['server'] = 'ProFTPd 1.3.4'
banner['up'] = 315.5
banner[200] = 'OK'
print(banner)
@studiawan
studiawan / loop.py
Last active March 11, 2021 04:58
Simple python loop
# pay attention to code indentation
# python uses indentation, not curly brackets like in C
# print 0 to 4
for i in range(5):
print(i)
# print 0 to 4 too
i = 0
while i < 5:
@studiawan
studiawan / client1.py
Last active March 15, 2021 07:43
Simple client sends string to server
import socket
# create socket and connect to server
server_address = ('localhost', 5000)
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(server_address)
# send string to server and close socket
client_socket.send(b'Hi ... ')
client_socket.close()
@studiawan
studiawan / server1.py
Last active March 11, 2021 06:21
Simple server receives one message from client and it's done.
import socket
# define server address, create socket, bind, and listen
server_address = ('localhost', 5000)
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(server_address)
server_socket.listen(1)
# accept client, receive its message and print
client_socket, client_address = server_socket.accept()
@studiawan
studiawan / server2.py
Last active March 11, 2021 06:34
Server always accept client until keyboard interrupt
import socket
import sys
# define server address, create socket, bind, and listen
server_address = ('localhost', 5000)
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(server_address)
server_socket.listen(5)
# infinite loop accepting client
@studiawan
studiawan / branch.py
Last active March 11, 2021 06:15
Simple Python branching
i = 5
# check i value
if i == 5:
print("It is 5")
else:
print("It is not 5")
# print 5 to 9 and say hello when it is 7
for i in range(5, 10):
@studiawan
studiawan / read.py
Last active December 24, 2015 15:19
Read text file using Python
# very simple way: open-read-close
f = open('textfile.txt', 'r')
data = f.read()
f.close()