17/03/20181INTRODUCTION TOPROGRAMMING INPYTHONWhat can you do with Python?■ Create games■ Create web applications■ Maths applications217/03/201823What sort of language is Python?Explicitlycompiledto machinecodePurelyinterpretedC, C++,FortranShell,PerlExplicitlycompiledto bytecodeJava, C#Implicitlycompiledto bytecodePythonCompiled InterpretedInstalling Python■ Go to https://www.python.org/■ Download the latest installer for Windows or MAC■ Double click on the file you downloaded■ Follow the installation process■ At the end of this process, … Continue reading “What can you do with Python | My Assignment Tutor”
17/03/20181INTRODUCTION TOPROGRAMMING INPYTHONWhat can you do with Python?■ Create games■ Create web applications■ Maths applications217/03/201823What sort of language is Python?Explicitlycompiledto machinecodePurelyinterpretedC, C++,FortranShell,PerlExplicitlycompiledto bytecodeJava, C#Implicitlycompiledto bytecodePythonCompiled InterpretedInstalling Python■ Go to https://www.python.org/■ Download the latest installer for Windows or MAC■ Double click on the file you downloaded■ Follow the installation process■ At the end of this process, Python IDLE should be installed417/03/20183IDE■ You can write python programs in any text editor■ But using an IDE makes your programming life easier■ But What is an IDE?■ IDE stands for Integrated Development Environment■ When you install Python it comes with an IDE called IDLE■ You can also use other IDE’sTwo modes of Python■ Interactive or immediate mode– At the prompt of you can directly type in Pythonexpressions and press enter to get the output■ Script mode■ In this mode, your Python program is written in a file.– The script file must have the extension .py– To execute this file in script mode we simply write pythonfilename.py at the command prompt.17/03/20184Your first Python Program■ Interactive Python■ Start Python■ This is the Python shell■ The three greater-than signs () are called the prompt■ For our first program, We will write some commands at the prompt7Your first Python Program■ print is a python command. It prints everything between the quotationmarks to the screen■ A Python command or function tells the computer what to do■ Python is thus a computer language that helps you to write in a languagethat you and the computer can understand.■ Python is case sensitive so print is not the same as Print817/03/20185Python operations OperationSymbolAddition+Subtraction–Multiplication*Division/Integer division//Raising to power**Remainder of a division (Modulus operator)% Order of operations■ BODMAS applies in Python1. Brackets2. Order (power)3. Division & Multiplication4. Addition and Subtraction17/03/20186Why should you care aboutBODMAS?■ For example, say you have 1 brother and 3 sisters and 8 candies,and you want to split the candies equally among your 4 siblings?■ Can you see the implications of not following BODMAS rules?■ Try this out: Let’s say your friend is trying to use JavaScript towork out how many balloons to buy. She’s throwing a party andwants everyone to have 2 balloons to blow up. There wereoriginally 15 people coming, but then she invited 9 more.■ Ans: 15+9*2 or (15+9)*2?Quitting Python exit() quit() Ctrl + DAny one of these will allowyou close python17/03/20187Data types■ Programming is all about manipulating data■ But what is data?■ Data is information that we store in our computer programs■ Examples of data:– your name– your age– Your address– Your gender– How many siblings you have, these things are all dataData types in Python■ There are three basic data types in Python– Numbers: used for representing numbers e.g. your age,your height– Strings: used to represent text e.g. your name– Booleans are values that can be true or false. E.g.whether you wear glasses or not■ All data in Python is just a combination of these types of data17/03/20188Operations on data■ Different operations apply to different data types■ As we saw earlier you can add, subtract, multiply or dividenumbers■ But you cant multiply strings■ There are other operations you can apply on strings as we willsee laterVariables■ Variables in programming refer to names of memory spaces■ You can think of a variable as a box that you can fit one thing in■ You can store information such as numbers, text, list and others invariables■ To create a new variable, you assign a value to the name of thevariable.■ The variable takes the type of the variable it is assigned to (Pythondynamically typed language)■ X = 1■ Y = “Hello world”■ X is a number and Y is a string or text.17/03/20189Variables■ The equal sign is called the assignment operator■ age = 5 means variable x is assigned a value of 5■ We can change the value of variables. In the above example agehas changed from 5 to 7.Variables■ Exercise:■ By creating variables as – secondsInAMinute, minutesinOneHour,print the number of seconds in a day, a week and a year.■ How old are you in hours, minutes and seconds?17/03/201810Naming variables■ Variables can not start with numbers■ Variables van not have spaces, if you have multi-word variable,then follow the camel convention, i.e. no spaces between eachword, capitalise the first character of each word except for the firstword.■ E.g. thisVariableFollowsCamelConvention■ Note that Python is case sensitive so age is not the same as Age.Key words and identifiers■ Keywords are reserved words in Python.■ Key words have meaning in the language so they can not be usedfor variables.■ Some Python keywords include: if, for, while, return, is, not, and,elif, else, break, continue, True, False, None, in, as, try, with, del,from and some more others.■ Identifiers: Identifier is the name given to entities like class,functions, variables etc.■ Identifiers are used to differentiate one entity from another.■ Keywords cannot be used as identifiers17/03/201811#Comments in python■ Did you notice the hash (#) in the code in the earlier slide?■ It starts with #■ Comments in Python start with # and are ignored by the Pythoninterpreter■ Comments are used to annotate your scripts so that they can beeasily understood by the reader which could be another person orthe future you■ What and where should I write comments?■ Don’t write comments for every line of code, this is not necessary.The code should be written to be self explanatory by usingmeaningful identifier names.■ Do write comments to explain a logic which may not be clear ormore importantly why you have done is the way you have.Multiline comments■ If we have comments that extend multiple lines use hash (#) at thebeginning of each line. For example:#This is a comment#that spans over#multiple lines■ Or using triple quotes, either ”’ or “””.“””This is also aperfect example ofmulti-line comments”””17/03/201812Syntax & Semantics■ Syntax:– The structure of strings in some language. A language’s syntaxis described by a grammar.– Examples:■ Binary number Identifier= | = 0 | 1■ = { | }= a | b | . . . | z> Run Module or with the script file open, just press F5.