bmstu.py lecture 1

aori.ru

			bmstu.py
		

Что за ужас тут творится?

Что за ужас тут творится?

Кстати

Следующее занятие пройдёт в пятницу (9 октября).

Кстати

Следующее занятие пройдёт в среду.
Из-за

План лекции

План лекции

Python

Релизации

Релизации

Версии

2.7.10 3.5.0

Документация

https://docs.python.org/2/library/
				int get_smallest_value(const std::vector& intvec)
				{
				    int smallest = std::numeric_limits::max(); 
				    for (int i = 0; i < intvec.size(); ++i)
				    {
				        smallest = std::min(smallest, intvec[i]);
				    }
				    return smallest;
				}
				

нет, это не production ready код

					int get_smallest_value(intvec)
					{
					    smallest = std::numeric_limits::max(); 
					    for (i = 0; i < intvec.size(); ++i)
					    {
					        smallest = std::min(smallest, intvec[i]);
					    }
					    return smallest;
					}
					

нет, это не production ready код

					def get_smallest_value(intvec)
					{
					    smallest = std::numeric_limits::max(); 
					    for (i = 0; i < intvec.size(); ++i)
					    {
					        smallest = std::min(smallest, intvec[i]);
					    }
					    return smallest;
					}
					

нет, это не production ready код

					def get_smallest_value(intvec)
					{
					    smallest = sys.maxint; 
					    for (i = 0; i < intvec.size(); ++i)
					    {
					        smallest = min(smallest, intvec[i]);
					    }
					    return smallest;
					}
					

нет, это не production ready код

					def get_smallest_value(intvec)
					{
					    smallest = sys.maxint; 
					    for item in intvec
					    {
					        smallest = min(smallest, item);
					    }
					    return smallest;
					}
					

нет, это не production ready код

					def get_smallest_value(intvec):
					    
					    smallest = sys.maxint; 
					    for item in intvec:
					    
					        smallest = min(smallest, item);
					    
					    return smallest;
					 
					

нет, это не production ready код

					def get_smallest_value(intvec):
					    smallest = sys.maxint; 
					    for item in intvec:
					        smallest = min(smallest, item);
					    return smallest;
					

нет, это не production ready код

					def get_smallest_value(intvec):
					    smallest = sys.maxint
					    for item in intvec:
					        smallest = min(smallest, item)
					    return smallest
					

нет, это не production ready код

					def get_smallest_value(vector):
					    smallest = sys.maxint
					    for item in vector:
					        smallest = min(smallest, item)
					    return smallest
					

нет, это не production ready код

					def get_smallest_value(vector):
					    return min(vector)
					

нет, это не production ready код

				min(vector)
			

Базовые типы данных

Базовые типы данных

Числа

Числа

Числа

Строки

			'This is a string'
			"This is also a string"
			'''This is a multiline
			string.'''
			u"Патриотичная строка"
		

Строки

			'This is a string'
			"This is also a string"
			'''This is a multiline
			string.'''
			u"Патриотичная строка"
		

Списки

			>>> a = [0, 1, 'abc', None]
			>>> a[0]
			0
			>>> a[0:2]
			[0, 1]
			>>> a.append('new_element')
			>>> a
			[0, 1, 'abc', None, 'new_element']
		

Списки

			 
			>>> a[0]  # это ввод
			0  # это вывод
			 
			 
			 
			 
			 
		

Списки

			>>> a = [0, 1, 'abc', None]
			>>> a[0]
			0
			>>> a[0:2]
			[0, 1]
			>>> a.append('new_element')
			>>> a
			[0, 1, 'abc', None, 'new_element']
		

кстати, список - это не тот список, про который рассказывали на "алгоритмах и структурах данных"

Словари

			>>> d = {'a': 11, 'b': 19, 19: None}
			>>> d['a']
			11
			>>> d['non-existing']
			KeyError
			>>> d.get('non-existing', 0)
			0
		

Словари

			>>> d = {'a': 11, 'b': 19, 19: None}
			>>> d.keys()
			['a', 19, 'b']
			>>> d.values()
			[11, None, 19]
			>>> d.items()
			[('a', 11), (19, None), ('b', 19)]
		

Словари

			>>> d = {'a': 11, 'b': 19, 19: None}
			>>> d.keys()
			['a', 19, 'b']
			>>> d.values()
			[11, None, 19]
			>>> d.items()
			[('a', 11), (19, None), ('b', 19)]
		

Синтаксические конструкции

for

			audios = [{'artist': u'Guns N\' Roses', 'title': u'Civil War'},
			          {'artist': u'Guns N\' Roses', 'title': u'Chinese Democracy'},
			          {'artist': u'Tito And Tarantula', 'title': u'After Dark'}]
			for audio in audios:
			    print audio['title'] + ' (' + audio['artist'] + ')'
		

for

			audios = [{'artist': u'Guns N\' Roses', 'title': u'Civil War'},
			          {'artist': u'Guns N\' Roses', 'title': u'Chinese Democracy'},
			          {'artist': u'Tito And Tarantula', 'title': u'After Dark'}]
			for audio in audios:
			    print audio['title'] + ' (' + audio['artist'] + ')'
		
			Civil War (Guns N' Roses)
			Chinese Democracy (Guns N' Roses)
			After Dark (Tito And Tarantula)
		

for

			audios = [{'artist': u'Guns N\' Roses', 'title': u'Civil War'}, ... ]
			audios_counter = 1
			for audio in audios:
			    print str(audios_counter) + '. ' + audio['title'] + ' (' + audio['artist'] + ')'
			   audios_counter = audios_counter + 1
		
			1. Civil War (Guns N' Roses)
			2. Chinese Democracy (Guns N' Roses)
			3. After Dark (Tito And Tarantula)
		

for

			audios = [{'artist': u'Guns N\' Roses', 'title': u'Civil War'}, ... ]
			audios_counter = 1
			for audio in audios:
			    print str(audios_counter) + '. ' + audio['title'] + ' (' + audio['artist'] + ')'
			    audios_counter = audios_counter + 1
		

for

			audios = [{'artist': u'Guns N\' Roses', 'title': u'Civil War'}, ... ]
			audios_counter = 1
			for audio in audios:
			    print '%s. %s (%s)' % (
			        audios_counter, audio['title'], audio['artist']
			    )
			    audios_counter = audios_counter + 1
		

for

			audios = [{'artist': u'Guns N\' Roses', 'title': u'Civil War'}, ... ]
			for audios_counter, audio in enumerate(audios):
			    print '%s. %s (%s)' % (
			        audios_counter + 1, audio['title'], audio['artist']
			    )
		

if

			audios = [{'artist': u'Guns N\' Roses', 'title': u'Civil War'}, ... ]
			for audios_counter, audio in enumerate(audios):
			    print '%s. %s (%s)' % (
			        audios_counter + 1, audio['title'], audio['artist']
			    )
		

if

			audios = [{'artist': u'Guns N\' Roses', 'title': u'Civil War'}, ... ]
			guns_n_roses_audios = []
			for audio in audios:
			    if audio['artist'] == 'Guns N\' Roses':
			        guns_n_roses_audios.append(audio)
			for audios_counter, audio in enumerate(guns_n_roses_audios):
			    print '%s. %s (%s)' % (
			        audios_counter + 1, audio['title'], audio['artist']
			    )
		

if

			 
			guns_n_roses_audios = []
			for audio in audios:
			    if audio['artist'] == 'Guns N\' Roses':
			        guns_n_roses_audios.append(audio)
		

list comprehension

			 
			guns_n_roses_audios = []
			for audio in audios:
			    if audio['artist'] == 'Guns N\' Roses':
			        guns_n_roses_audios.append(audio)
		
			[audio for audio in audios if audio['artist'] == 'Guns N\' Roses']
		

list comprehension

			 
			guns_n_roses_audios = []
			for audio in audios:
			    if audio['artist'] == 'Guns N\' Roses':
			        guns_n_roses_audios.append(audio)
		
			[audio for audio in audios if audio['artist'] == 'Guns N\' Roses']
		

list comprehension

			 
			guns_n_roses_audios = []
			for audio in audios:
			    if audio['artist'] == 'Guns N\' Roses':
			        guns_n_roses_audios.append(audio)
		
			[audio for audio in audios if audio['artist'] == 'Guns N\' Roses']
		

list comprehension

			 
			guns_n_roses_audios = []
			for audio in audios:
			    if audio['artist'] == 'Guns N\' Roses':
			        guns_n_roses_audios.append(audio)
		
			[audio for audio in audios if audio['artist'] == 'Guns N\' Roses']
		

list comprehension

			 
			guns_n_roses_audios = []
			for audio in audios:
			    if audio['artist'] == 'Guns N\' Roses':
			        guns_n_roses_audios.append(audio)
		
			[audio for audio in audios if audio['artist'] == 'Guns N\' Roses']
		

list comprehension

			audios = [{'artist': u'Guns N\' Roses', 'title': u'Civil War'}, ... ]
			guns_n_roses_audios = [a for a in audios if a['artist'] == 'Guns N\' Roses']
			for audios_counter, audio in enumerate(guns_n_roses_audios):
			    print '%s. %s (%s)' % (
			        audios_counter + 1, audio['title'], audio['artist']
			    )
		

functions

			def pretty_print_audios(audios):
			    guns_n_roses_audios = [a for a in audios if a['artist'] == 'Guns N\' Roses']
			    for audios_counter, audio in enumerate(guns_n_roses_audios):
			        print '%s. %s (%s)' % (
			            audios_counter + 1, audio['title'], audio['artist']
			        )
		

functions

			def pretty_print_audios_for_artist(audios, artist_name):
			    artists_audios = [a for a in audios if a['artist'] == artist_name]
			    for audios_counter, audio in enumerate(artists_audios):
			        print '%s. %s (%s)' % (
			            audios_counter + 1, audio['title'], audio['artist']
			        )
		

functions

			def pretty_print_audios_for_artist(audios, artist_name, max_len=10):
			    artists_audios = [a for a in audios if a['artist'] == artist_name]
			    for audios_counter, audio in enumerate(artists_audios[:max_len]):
			        print '%s. %s (%s)' % (
			            audios_counter + 1, audio['title'], audio['artist']
			        )
		

functions

			def pretty_print_audios_for_artist(audios, artist_name, max_len=10):
			    artists_audios = [a for a in audios if a['artist'] == artist_name]
			    for audios_counter, audio in enumerate(artists_audios[:max_len]):
			        print '%s. %s (%s)' % (
			            audios_counter + 1, audio['title'], audio['artist']
			        )
		

slicing

			>>> a = range(10)  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
			>>> a[2:7]
			[2, 3, 4, 5, 6]
			>>> a[2:7:2]
			[2, 4, 6]
			>>> a[0:3]
			[0, 1, 2]
			>>> a[6:10]
			[6, 7, 8, 9]
		

slicing

			>>> a = range(10)  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
			>>> a[2:7]
			[2, 3, 4, 5, 6]
			>>> a[2:7:2]
			[2, 4, 6]
			>>> a[0:3]
			[0, 1, 2]
			>>> a[6:10]
			[6, 7, 8, 9]
		

slicing

			>>> a = range(10)  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
			>>> a[2:7]
			[2, 3, 4, 5, 6]
			>>> a[2:7:2]
			[2, 4, 6]
			>>> a[:3]
			[0, 1, 2]
			>>> a[6:]
			[6, 7, 8, 9]
		

slicing

			>>> a = range(10)  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
			>>> a[:100]
		

slicing

			>>> a = range(10)  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
			>>> a[:100]
			[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
		

slicing

			>>> a = range(10)  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
			>>> a[:]
		

slicing

			>>> a = range(10)  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
			>>> a[:]
			[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
		

slicing

			>>> a = range(10)  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
			>>> a[::2]
		

slicing

			>>> a = range(10)  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
			>>> a[::2]
			[0, 2, 4, 6, 8]
		

slicing

			>>> a = range(10)  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
			>>> a[::-1]
		

slicing

			>>> a = range(10)  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
			>>> a[::-1]
			[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
		

slicing

			>>> 'omg, this works on strings!'[::-1]
			'!sgnirts no skrow siht ,gmo'
		

functions

			form_data = {'email': 'bmstu.py@gmail.com', 'course': 3}
			 
			def is_form_data_valid(data):
			    ???
		

functions

			form_data = {'email': 'bmstu.py@gmail.com', 'course': 3}
			 
			def is_form_data_valid(data):
			    is_form_valid = True
			    if('@' not in data['email']):
			        is_form_valid = False
			    if (data['course'] < 2) or (data['course'] > 5):
			        is_form_valid = False
			    return is_form_valid
		

functions

			form_data = {'email': 'bmstu.py@gmail.com', 'course': 3}
			 
			def is_form_data_valid(data):
			    is_form_valid = True
			    if('@' not in data['email']):
			        is_form_valid = False
			    if (data['course'] < 2) or (data['course'] > 5):
			        is_form_valid = False
			    return is_form_valid
		

functions

			form_data = {'email': 'bmstu.py@gmail.com', 'course': 3}
			 
			def is_form_data_valid(data):
			    is_form_valid = True
			    if '@' not in data['email']:
			        is_form_valid = False
			    if data['course'] < 2 or data['course'] > 5:
			        is_form_valid = False
			    return is_form_valid
		

functions

			form_data = {'email': 'bmstu.py@gmail.com', 'course': 3}
			 
			def is_form_data_valid(data):
			    is_form_valid = True
			    if '@' not in data['email']:
			        is_form_valid = False
			    if not (2 <= data['course'] <= 5):
			        is_form_valid = False
			    return is_form_valid
		

functions

			form_data = {'email': 'bmstu.py@gmail.com', 'course': 3}
			 
			def is_form_data_valid(data):
			    is_form_valid = True
			    if '@' not in data['email'] or not (2 <= data['course'] <= 5):
			        is_form_valid = False
			    return is_form_valid
		

functions

			form_data = {'email': 'bmstu.py@gmail.com', 'course': 3}
			 
			def is_form_data_valid(data):
			    return not('@' not in data['email'] or not (2 <= data['course'] <= 5))
		

functions

			form_data = {'email': 'bmstu.py@gmail.com', 'course': 3}
			 
			def is_form_data_valid(data):
			    email, course = data['email'], data['course']
			    return not('@' not in email or not (2 <= course <= 5))
		

functions

			form_data = {'email': 'bmstu.py@gmail.com', 'course': 3}
			 
			def is_form_data_valid(data):
			    email, course = data['email'], data['course']
			    return is_email_correct(email) and is_course_correct(course)
			 
			def is_email_correct(email):
			    return '@' in email
			 
			def is_course_correct(course):
			    return 2 <= data['course'] <= 5
		

Модули

			# library.py
			def is_form_data_valid(data):
			    ...
		
			# script.py
			form_data = request.get_data()
			if is_form_data_valid(form_data):
			    ...
		

Модули

			# library.py
			def is_form_data_valid(data):
			    ...
		
			# script.py
			from library import is_form_data_valid
			form_data = request.get_data()
			if is_form_data_valid(form_data):
			    ...
		

Модули

			# library.py
			def is_form_data_valid(data):
			    ...
		
			# script.py
			from library import is_form_data_valid
			form_data = request.get_data()
			if is_form_data_valid(form_data):
			    ...
		

Модули

			# library.py
			def is_form_data_valid(data):
			    ...
		
			# script.py
			from library import is_form_data_valid
			form_data = request.get_data()
			if is_form_data_valid(form_data):
			    ...
		

Модули

			# library.py
			def is_form_data_valid(data):
			    ...
		
			# script.py
			import library
			form_data = request.get_data()
			if library.is_form_data_valid(form_data):
			    ...
		

CLI

			ls cd mkdir cp mv rm cat tail grep
		
			python pip
		

Python

			$ python hello_world.py
			Hello world!
		
			$ python
			Python 2.7.6 (default, Sep  9 2014, 15:04:36)
			[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
			Type "help", "copyright", "credits" or "license" for more information.
			>>> 
		

Заключительное слово

Практикум

1. Hello world

2. Output audios

3. Count audios

4. Testing count audios

5. User authentication

6. Blog

7. Advanced blog