class: center, middle .center[![Python](python.png)] # Python Course: Basics Shahid Beheshti University Instructor: S. M. Masoud Sadrnezhaad --- class: split-50 # Why should you learn to write programs?
.column[ - Computers are built for one purpose - to **do things for us**. - But we need to **speak their language** to describe what we want done. - Users have it easy - someone already put many different **programs (instructions)** into the computer and users just pick the ones they want to use. ] .column[ .center[![Alt text](what_next.png)] .center[![Alt text](hardware_architecture.png)] ]
--- class: split-70 # What's Python
.column[ - Python is a widely used **general-purpose** **interpreted**, **object-oriented**, **very-high-level** programming language. - It was created by **Guido van Rossum** during 1985- 1990. - Python source code is also available under the **GNU General Public License (GPL)**. - Language is **named after the BBC show** “Monty Python’s Flying Circus”. ] .column[ .center[![Guido](guido.jpg)] ]
.center[![Interpreter](interpreter.png)] --- class: split-50 # What's Python (Contd.)
.column[ - 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. - It features **automatic memory management** and has a **large and comprehensive standard library**. - Python interpreters are available for **many operating systems**. ] .column[ * Java ```java public class HelloWorld { public static void main(String args) { System.out.println("Hello, world!"); } } ``` * C ```c #include
int main(void) { printf("Hello, world!\n"); } ``` * Python ```python print("Hello, world!") ``` ]
--- # 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.10 at the moment) --- # Python Installation - Go to http://www.python.org and follow the instructions for installing Python 3. - Python Shell - PyCharm IDE .center[![Interpreter](pycharm.png)] --- # 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' ``` --- # String Formatting - % operator ```python >>> 'Number of %s is %d.' % ('steps', 10) Number of steps is 10. ``` - `.format` method ```python >>> 'Number of {0} is {1}.'.format('steps', 10) Number of steps is 10. ``` - formatted string (Python 3.6+) ```python >>> var = Python >>> f'Hello, {var}!' 'Hello, Python!' ``` --- # References - Dr. Hamid Zarrabi-Zadeh's Web Course, Fall 2013. - https://www.tutorialspoint.com/python3/index.htm - http://www.diveintopython3.net/ - http://openbookproject.net/thinkcs/python/english3e/ - http://en.wikipedia.org/wiki/Python_(programming_language) - https://www.coursera.org/learn/python --- class: center, middle .center[![Python](python.png)] # Thank you. Any questions?