web

Python запити


Посилання на Online Python


https://www.w3schools.com - Python Compiler (Editor)

https://www.online-python.com - Online Python beta

https://onecompiler.com - Python Online Compiler





Код пошуку найчастіше зустрічається слова в текстовому файлі:

          
name = input('Enter file:')
handle = open(name, 'r')
counts = dict()
for line in handle:
    words = line.split()
for word in words:
    counts[word] = counts.get(word, 0) + 1
    bigcount = None
    bigword = None
for word, count in list(counts.items()):
    if bigcount is None or count > bigcount:
        bigword = word
        bigcount = count
    print(bigword, bigcount)
# Вихідний код: http://www.py4e.com/code3/words.py

Код з офсайту python.org:


def fib(n):
  a, b = 0, 1
  while a < n:
      print(a, end=' ')
      a, b = b, a+b
  print()
fib(1000)

FOR - коли кількість ітерацій фіксована, більш чистіший і коротший код, кращій вибір

WHILE - коли кількість ітерацій вираховується під час виконання коду

type() відобразить тип


# type() відобразить тип
print( type('Hello, World!') )
print( type(555) )

######################################################
## Підрахунок символів, що йдуть поспіль, у рядку
######################################################
text = 'ФОП Натташшшша'
sq = []
cnt2 = 0
cnt = 0
ch = text[0]
for c in text[1:]:    
    if c != ch:
        if cnt > 1:
            sq.append(ch)
            sq.append(cnt)
        ch = c
        cnt = 1
    else:
        cnt+=1
print(text, sq) 

Масове перейменування файлів


# https://youtu.be/rQLUBJ8t7C8
import os
 
dir = 'file_rename\Avatars'
list = os.listdir(dir)
# print(len(list))
# old_file = os.path.join(dir, 'filename.txt')
# print(old_file)
# new_file = os.path.join(dir, 'new_filename.txt')
 
# os.rename(old_file, new_file)
n = 1
for old_file in list:    
    old_file = os.path.join(dir, old_file)
    new_file = os.path.join(dir, str(n) + '-avatar.jpg')
    print(old_file + ' ---> ' + new_file)
    os.rename(old_file, new_file)
    n +=1

Pass generator


import random

lower_case = "abcdefghijklmnopqrstuvwxyz"
upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
number = "0123456789"
symbols = "@#$%&*/\?"

use_for = lower_case + upper_case + number + symbols
length_for_pass = 8

password = "".join(random.sample(use_for, length_for_pass))

print("Your generated password:", password)