you need? § Python as a terminal § Python as a script § Types of variables § Loops and Control § Using Methods and Libs § Gathering Information § Python Vs iPython 13/02/2017 Python Bootcamp - Basic I 2
Python • Python Libs • Text Editors • Integrated Development Environment (IDE) 13/02/2017 Python Bootcamp - Basic I 9 Windows MacOs Linux TM Distributions Linux Mint TM Ubuntu TM Fedora TM
Basic I 18 $ python Start typing >>> print(“Hello World”) Hello World Say “Hello World!” >>> x = 2 >>> print(x) 2 Assign a variable Python 2.x and 3.x!
23 01 #!/path/to/python 02 # -*- coding: utf8 -*- 03 “““ 04 This is a docstring where information 05 about what you are doing and be written. 06 ””” 07 print(“Hello World!”) File “say_hello_world.py” path_to_file $ python say_hello_world.py Hello World! To run this file:
Integers >>> x = 2 >>> print type(x) <type 'int'> >>> >>> x = x + 2 >>> print x.__class__ <type 'int'> >>> x = 2.0 >>> print type(x) <type 'float'> >>> >>> y = 1 >>> y = x + y >>> print y.__class__ <type 'float'> Float/Double Any assigned variable in Python is an object and objects have attributes. Use .__class__ atribute to find what type is your object.
fruit != ‘maçã’ True Types of variables 13/02/2017 Python Bootcamp - Basic I 29 More on boolean operators (like +, *, ==, !=, <, >, <=, >=, is, not) HERE Booleans
13/02/2017 Python Bootcamp - Basic I 32 More on boolean operators (like +, *, ==, !=, <, >, <=, >=, is, not) HERE Types of variables >>> True * False False >>> True and False False Booleans >>> not False True >>> not True False
operators (like +, *, ==, !=, <, >, <=, >=, is, not) HERE Types of variables >>> x = 1 >>> y = 1 >>> x == y True >>> x is y False >>> x = 1 >>> y = x >>> x == y True >>> x is y True Booleans
operators (like +, *, ==, !=, <, >, <=, >=, is, not) HERE Types of variables >>> x = 1 >>> y = 1 >>> x == y True >>> x is y False >>> x = 1 >>> y = x >>> x == y True >>> x is y True Booleans
“I'm another string” Strings Types of variables 13/02/2017 Python Bootcamp - Basic I 47 Strings behaves like lists >>> s[3] 'm' >>> s + r “I am a stringI'm another string” >>> 5 * “xy” 'xyxyxyxyxy'
“I'm another string” Strings Types of variables 13/02/2017 Python Bootcamp - Basic I 48 Strings behaves like lists >>> s[3] 'm' >>> s + r “I am a stringI'm another string” >>> 5 * “xy” 'xyxyxyxyxy'
“I'm another string” Strings Types of variables 13/02/2017 Python Bootcamp - Basic I 49 Strings behaves like lists >>> s[3] 'm' >>> s + r “I am a stringI'm another string” >>> 5 * “xy” 'xyxyxyxyxy' Formating strings like C printf >>> x = 7 >>> print(“%03d” % x) 003 http://pt.wikipedia.org/wiki/Printf http://www.cplusplus.com/reference/cstdio/printf/
“I'm another string” Strings Types of variables 13/02/2017 Python Bootcamp - Basic I 50 Strings behaves like lists >>> s[3] 'm' >>> s + r “I am a stringI'm another string” >>> 5 * “xy” 'xyxyxyxyxy' Or using .format() >>> x = 7 >>> print(“{:03d}”.format(x)) 003 Python – Format mini language specs
Dictionaries >>> fruits = {‘apple’: 3, ‘orange’: 1.5} >>> print fruits[‘apple’] 3 >>> print ‘apple’ in fruits True >>> print ‘kiwi’ in fruits False Creating a dictionary and accessing its elements Check if a dictionary has an element