Installation, Sample Usage, Strings and OOP Telerik Software Academy Software Quality Assurance
Installing and Running Python Fundamentals of Python development Data types Control structures: loops, conditionals Functions Object-oriented programming
On MAC, Linux and Windows
On MAC Homebrew can be used $ brew install python On Linux Part of Linux is built with Python, so it comes out- of-the-box On Windows Download the installer from Add the installation path to System Variables $PATH
Live Demo
Open the CMD/Terminal and run: You can run python code: Print the numbers from 0 to 4 $ python for i in range(5): print(i) print(i) sum = 0 for i in range(5, 10): sum += I sum += Iprint(sum) Sum the numbers from 5 to 9 Significant whitespace matters a lot
Significant whitespace is a way to write code in python This is actually the identation Significant whitespace creates blocks in python code It is the equivalent of curly brackets ({ }) in other languages
Data types, Control Structures and more
Python supports all the primary data types: int - integer numbers int(intAsString) parses a string to int float - floating-point numbers float(floatAsString) parses a string to float none – like null bool – Boolean values (True and False)
Live Demo
Python supports conditionals: if conditionOne: # run code if conditionOne is True # run code if conditionOne is True elif conditionTwo: # run code if conditionOne is False # run code if conditionOne is False # but conditionTwo is True # but conditionTwo is True else # run code if conditionOne and # conditionTwo are False The conditions are True-like and False-like i.e. "String", 0, none are evaluated to True While ""(empty string), any number or object are evaluated to False
Live Demo
There are two types of loops in Python for loop for i in range(5): # run code with values of i: 0, 1, 2, 3 and 4 # run code with values of i: 0, 1, 2, 3 and 4 names = ['Doncho', 'Asya', 'Evlogi', 'Nikolay', 'Ivaylo'] for i in range(len(names)): print('Hi! I am %s!'%names[i]); print('Hi! I am %s!'%names[i]); while loop number = 1024 binNumber = ''; while number >= 1: binNumber += '%i'%(number%2) binNumber += '%i'%(number%2) number /= 2 number /= 2print(binNumber[::-1])
Live Demo
Separate the code in smaller and reusable pieces
Functions in Python: Are defined using the keyword "def" Have an identifier A list of parameters A return type def toBin(number): bin = '' bin = '' while number >= 1: while number >= 1: bin += '%i'%(number%2) bin += '%i'%(number%2) number /= 2 number /= 2 return bin[::-1] return bin[::-1]
Live Demo
How to separate the code in different modules
Python applications are separated into modules These module represent just a list of functions and or classes To use all functions from a module numeralSystems : import numeralSystems print(numeralSystems.toBin(1023))print(numeralSystems.toHex(1023)) from numeralSystems import toBin as bin A single function: Or some functions from numeralSystems import toBin as bin, toHex as hex
Live Demo
Python has classes and inheritance class Person: lastPersonId = 0 lastPersonId = 0 def __init__(self, name): def __init__(self, name): self.__id = ++lsatPersonId self.__id = ++lsatPersonId self.__name = name self.__name = name def getId(): def getId(): return self.__id return self.__id def getName(): def getName(): return self.__name return self.__name def setName(newName): def setName(newName): self.__name = newName self.__name = newName
Live Demo
форум програмиране, форум уеб дизайн курсове и уроци по програмиране, уеб дизайн – безплатно програмиране за деца – безплатни курсове и уроци безплатен SEO курс - оптимизация за търсачки уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop уроци по програмиране и уеб дизайн за ученици ASP.NET MVC курс – HTML, SQL, C#,.NET, ASP.NET MVC безплатен курс "Разработка на софтуер в cloud среда" BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране, книги – безплатно от Наков безплатен курс "Качествен програмен код" алго академия – състезателно програмиране, състезания ASP.NET курс - уеб програмиране, бази данни, C#,.NET, ASP.NET курсове и уроци по програмиране – Телерик академия курс мобилни приложения с iPhone, Android, WP7, PhoneGap free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране