class: center, middle .center[] # Python Course: Basics Shahid Beheshti University Instructor: S. M. Masoud Sadrnezhaad --- # What's Python - Python is a widely used **general-purpose**, **high-level** programming language. - Its design philosophy emphasizes **code readability**. - Its syntax allows programmers to express concepts in **fewer lines of code** than would be possible in languages such as C++ or Java. - Python supports **multiple programming paradigms**. - It features a **dynamic type system** and **automatic memory management** and has a **large and comprehensive standard library**. - Python interpreters are available for **many operating systems**. --- # Python 2.x vs 3.x - Python 2.0 was released in 2000, with many new features added. - Python 3.0, adjusting several aspects of the core language, was released in 2008. - Python 3.0 is **backwards-incompatible**. - Python 2.x is legacy, Python 3.x is the present and future of the language. - We use Python 3.x in this course. (3.7.0 at the moment) --- # Python Installation - Go to http://www.python.org and follow the instructions for installing Python 3. - Python Shell - PyCharm IDE --- # Hello, world! ```python print("Hello, world!") ``` --- # Let's see something serious! - No semi-colons. Indentation matters. ```python SUFFIXES = {1000: ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], 1024: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']} def approximate_size(size, a_kilobyte_is_1024_bytes=True): '''Convert a file size to human-readable form. Keyword arguments: size -- file size in bytes a_kilobyte_is_1024_bytes -- if True (default), use multiples of 1024 if False, use multiples of 1000 Returns: string ''' if size < 0: raise ValueError('number must be non-negative') multiple = 1024 if a_kilobyte_is_1024_bytes else 1000 for suffix in SUFFIXES[multiple]: size /= multiple if size < multiple: return '{0:.1f} {1}'.format(size, suffix) raise ValueError('number too large') if __name__ == '__main__': print(approximate_size(1000000000000, False)) print(approximate_size(1000000000000)) ``` --- # Variables - Python is a dynamically typed language. - Python has has typed objects but untyped variable names. ```python >>> my_var = 2 >>> type(my_var)
>>> my_var = "hi there!" >>> type(my_var)
``` --- # Basic Data Types Booleans: True, False - bool Strings: "a string", 'another string' - str Numbers: 42, 159.523, 4j - int, float, complex None --- # Operators - Basic operators - Arithmetic (`+`, `-`, `*`, `**`, `/`, `%`, `//`) - Assignment (`=`, `+=`, `-=`, `*=`, `**=`, `/=`, `%=`, `//=`) - Comparison (`<`, `>`, `<=`, `>=`, `==`, `!=`) - Logical (`and`, `or`, `not`) - Bitwise operators - `x << y`: Returns x with the bits shifted to the left by y places (and new bits on the right-hand-side are zeros). This is the same as multiplying x by `2**y`. - `x >> y`: Returns x with the bits shifted to the right by y places. This is the same as //'ing x by `2**y`. - `x & y`: Does a "bitwise and". - `x | y`: Does a "bitwise or". - `~ x`: Returns the complement of x two’s complement. - `x ^ y`: Does a "bitwise exclusive or". --- # Type Conversion - Python is **strongly typed**, forbidding operations that are not well-defined, rather than silently attempting to make sense of them (compare with JavaScript). ```python >>> 'Masoud' + ' ' + 'Sadrnezhaad' 'Masoud Sadrnezhaad' >>> 'ha' * 10 'hahahahahahahahahaha' >>> 'ten' + 5 Traceback (most recent call last): File "
", line 1, in
TypeError: Can't convert 'int' object to str implicitly >>> 'ten' + str(5) 'ten5' ``` --- # Truth Value Testing - Any object can be tested for truth value, for use in a condition. - The following values are considered False - None - False - 0, 0.0, 0j - Any empty sequence, set or dictionary: - '', "", {}, [], (), etc. - The rest is True. --- # References - Dr. Hamid Zarrabi-Zadeh's Web Course, Fall 2013. - http://www.diveintopython3.net/ - http://openbookproject.net/thinkcs/python/english3e/ - http://en.wikipedia.org/wiki/Python_(programming_language) --- class: center, middle .center[] # Thank you. Any questions?