Download presentation
Presentation is loading. Please wait.
Published byBrent Simmons Modified over 9 years ago
1
By Austin Laudenslager AN INTRODUCTION TO PYTHON
2
Created in 1989 by Guido van Rossum Highly extensible Aims to create beautiful, readable code Variables are assigned a type automatically Blocks of code are defined by whitespace Arguments from the console are stored in the argv variable, which can be accessed using sys.argv[n] _ variable can be used in interactive mode to refer to the previous expression – it can not be assigned a value manually PYTHON
3
+, -, *, % work the same as C++/Java / returns a float, // performs integer division ** can be used to calculate powers Use parenthesis for grouping () MATH TIP: Integers and floating point numbers may both be used in the same equation without typecasting, computes to floating point number. >>>5+5>>>5*5>>>7%2 10251 >>>5/2>>>5//2 2.52 >>>5**2>>>10**10 25100 >>>5*2+2>>>5*(2+2) 1220
4
Can be enclosed in either single or double quotes \ can be used to escape quotes, \n is new line Use print() to display strings, r ignores special characters Triple quotes allows strings to span multiple lines STRINGS >>> ‘string’ >>> “string” >>>’I can\’t’>>>’Line one\nLine two’ ‘I can’t”Line one Line two >>>Print(‘some\name’)>>>print(r’some\name’) somesome\name ame >>>print(“”” Line one Line two “””) Line one Line two
5
+ allows for concatenation, * performs repetition Strings are indexed from the left AND right Use [n:m] to slice strings Use len() to return the length of a string STRING OPERATIONS >>> ‘string’ + ‘one’>>> ‘string’ * 3 ‘stringone’‘stringstringstring’ >>>word=‘Python’>>>word[0]>>>word[-2] ‘P’‘0’ >>>word[2:4]>>>word[:2] ‘th’‘Py’ >>>len(word) 6 TIP: Strings are immutable: Word[3] = ‘z’ will NOT work.
6
Declared as a list of comma seperated values Can be indexed, sliced, and concatenated the same as strings ARE mutable, can be changed by index OR slice Can use function append() to add items to the end of a list Funtion len() returns length of list Can nest lists: LISTS >>>numbers = [1, 2, 3]>>>strings = [‘a’, ‘b’, ‘c’] >>>number.append(1)>>>len(number) [4, 8, 7, 3, 1]5 >>>nest = [numbers, words] >>>nest[0]>>>nest[0][3] [4, 8, 7, 3, 1]3 >>>numbers[1] = 5>>>numbers[0:2] = [4, 8, 7] [1, 5, 3][4, 8, 7, 3]
7
While, if, elif, else work like in C++ Unlike C++, for loops iterate over items of a sequence numbers = [6, 2, 4] Use range(n) to iterate over a sequence of number Break statement ends current loop Else can also be used with loops Continue statement skips to the next iteration of the loop Pass can be used to represent a statement where no action is needed CONTROL FLOW STATEMENTS >>>for n in numbers: TIP: Control flow statements do NOT use parenthesis in Python. >>>for i in range(10) IMPORTANT: Python uses whitespace indentation, levels of code are determined by indentation, not grouped by curly braces!
8
All functions are of the def, or definition type Can return any type of object Returns none by default Example function: n can be of any type and will be returned as the same type Can also include optional arguments with default values: Can be called with multiply(n) or multiply(n,m) FUNCTIONS >>> def returnVal(n): print(n) return n >>> def multiply(n, count=2): return n*count
9
SOURCES https://docs.python.org/3/tutorial/index.html http://en.wikipedia.org/wiki/Python_%28programmi ng_language%29
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.