PYTHON NOTES What Is Python?
Total Page:16
File Type:pdf, Size:1020Kb
PYTHON NOTES What is Python? Python is a popular programming language. It was created in 1991 by Guido van Rossum. It is used for: web development (server-side), software development, mathematics, system scripting. What can Python do? Python can be used on a server to create web applications. Python can connect to database systems. It can also read and modify files. Python can be used to handle big data and perform complex mathematics. Python can be used for production-ready software development. Why Python? Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc). Python has a simple syntax similar to the English language. Python has syntax that allows developers to write programs with fewer lines than some other programming languages. Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This means that prototyping can be very quick. Good to know The most recent major version of Python is Python 3, which we shall be using in this tutorial. However, Python 2, although not being updated with anything other than security updates, is still quite popular. Python Syntax compared to other programming languages Python was designed to for readability, and has some similarities to the English language with influence from mathematics. Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses. Python relies on indentation, using whitespace, to define scope; such as the scope of loops, functions and classes. Other programming languages often use curly-brackets for this purpose. Python Install Many PCs and Macs will have python already installed. To check if you have python installed on a Windows PC, search in the start bar for Python or run the following on the Command Line (cmd.exe): C:\Users\Your Name>python --version To check if you have python installed on a Linux or Mac, then on linux open the command line or on Mac open the Terminal and type: python --version If you find that you do not have python installed on your computer, then you can download it for free from the following website: https://www.python.org/ Or Log on to https://www.learnpython.org/ to practice it online PRINTING HELLO WORLD!!! The simplest directive in Python is the "print" directive - it simply prints out a line (and also includes a newline, unlike in C). There are two major Python versions, Python 2 and Python 3. Python 2 and 3 are quite different. This tutorial uses Python 3, because it more semantically correct and supports newer features. For example, one difference between Python 2 and 3 is the print statement. In Python 2, the "print" statement is not a function, and therefore it is invoked without parentheses. However, in Python 3, it is a function, and must be invoked with parentheses. To print a string in Python 3, just write: print("Hello World!") This will print Hello World in the shell Whenever you are done in the python command line, you can simply type the following to quit the python command line interface: exit() Python Indentations Where in other programming languages the indentation in code is for readability only, in Python the indentation is very important. Python uses indentation to indicate a block of code. Example if 5 > 2: print("Five is greater than two!") Output Five is greater than two! Python will give you an error if you skip the indentation: if 5 > 2: print("Five is greater than two!") Output File "demo_indentation_test.py", line 2 print("Five is greater than two!") ^ IndentationError: expected an indented block Comments Python has commenting capability for the purpose of in-code documentation. Comments start with a #, and Python will render the rest of the line as a comment: Example Comments in Python: #This is a comment. print("Hello, World!") Output Hello, World! Exercise: Insert the missing part of the code below to output "Hello World". ("Hello World") Answer Print Creating Variables Unlike other programming languages, Python has no command for declaring a variable. A variable is created the moment you first assign a value to it. Example x = 5 y = "John" print(x) print(y) output 5 John Variables do not need to be declared with any particular type and can even change type after they have been set. Example x = 4 # x is of type int x = "Sally" # x is now of type str print(x) Output Sally Variable Names A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for Python variables: A variable name must start with a letter or the underscore character A variable name cannot start with a number A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) Variable names are case-sensitive (age, Age and AGE are three different variables) Remember that variables are case-sensitive Output Variables The Python print statement is often used to output variables. To combine both text and a variable, Python uses the + character: Example x = "awesome" print("Python is " + x) Output Python is awesome You can also use the + character to add a variable to another variable: Example x = "Python is " y = "awesome" z = x + y print(z) Output Python is awesome For numbers, the + character works as a mathematical operator: Example x = 5 y = 10 print(x + y) Output 15 If you try to combine a string and a number, Python will give you an error: Example x = 5 y = "John" print(x + y) Output TypeError: unsupported operand type(s) for +: 'int' and 'str' Test Yourself With Exercises Exercise: Create a variable named carname and assign the value Volvo to it. = " " Answer carname=”Volvo” Python Numbers There are three numeric types in Python: int float complex Variables of numeric types are created when you assign a value to them: Example x = 1 # int y = 2.8 # float z = 1j # complex To verify the type of any object in Python, use the type() function: Example print(type(x)) print(type(y)) print(type(z)) Output <class 'int'> <class 'float'> <class 'complex'> Int Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length. Example Integers: x = 1 y = 35656222554887711 z = -3255522 print(type(x)) print(type(y)) print(type(z)) Output <class 'int'> <class 'int'> <class 'int'> Float Float, or "floating point number" is a number, positive or negative, containing one or more decimals. Example Floats: x = 1.10 y = 1.0 z = -35.59 print(type(x)) print(type(y)) print(type(z)) output <class 'float'> <class 'float'> <class 'float'> Float can also be scientific numbers with an "e" to indicate the power of 10. Example Floats: x = 35e3 y = 12E4 z = -87.7e100 print(type(x)) print(type(y)) print(type(z)) output <class 'float'> <class 'float'> <class 'float'> Complex Complex numbers are written with a "j" as the imaginary part: Example Complex: x = 3+5j y = 5j z = -5j print(type(x)) print(type(y)) print(type(z)) output <class 'complex'> <class 'complex'> <class 'complex'> Specify a Variable Type There may be times when you want to specify a type on to a variable. This can be done with casting. Python is an object-orientated language, and as such it uses classes to define data types, including its primitive types. Casting in python is therefore done using constructor functions: int() - constructs an integer number from an integer literal, a float literal (by rounding down to the previous whole number), or a string literal (providing the string represents a whole number) float() - constructs a float number from an integer literal, a float literal or a string literal (providing the string represents a float or an integer) str() - constructs a string from a wide variety of data types, including strings, integer literals and float literals Example Integers: x = int(1) # x will be 1 y = int(2.8) # y will be 2 z = int("3") # z will be 3 Run example » Example Floats: x = float(1) # x will be 1.0 y = float(2.8) # y will be 2.8 z = float("3") # z will be 3.0 w = float("4.2") # w will be 4.2 Run example » Example Strings: x = str("s1") # x will be 's1' y = str(2) # y will be '2' z = str(3.0) # z will be '3.0' Strings are defined either with a single quote or a double quotes. The difference between the two is that using double quotes makes it easy to include apostrophes (whereas these would terminate the string if using single quotes) mystring = "Don't worry about apostrophes" print(mystring) Output Don't worry about apostrophes Whereas if mystring = ‘Don't worry about apostrophes’ it will give an error Python Operators Operators are used to perform operations on variables and values. Python divides the operators in the following groups: Arithmetic operators Assignment operators Comparison operators Logical operators Identity operators Membership operators Bitwise operators Python Arithmetic Operators Arithmetic operators are used with numeric values to perform common mathematical operations: Operator Name Example + Addition x + y - Subtraction x - y * Multiplication x * y / Division x / y % Modulus x % y ** Exponentiation x ** y // Floor division x // y Eg x = 5 y = 3 print(x * y) Output 15 Or x = 5 y = 2 print(x % y) Output 1 .