This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# declaration without elements | |
courses = [] | |
# declaration with elements | |
languages = ['python', 'java', 'c'] | |
print(languages) | |
# fill the list | |
courses.append('progjar') | |
courses.append('j2ee') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# very simple way: open-read-close | |
f = open('textfile.txt', 'r') | |
data = f.read() | |
f.close() |
OlderNewer