Summary

This document provides a basic introduction to Python coding. It covers concepts such as keywords, literals, and identifiers and provides guidelines for creating identifiers in Python. The document also includes a discussion of variables and data types in Python.

Full Transcript

Ex: 5 - Coding in Python  Python has evolved into a very useful language as it is simple and is being used in many fields such as in Robotics, Scientific Purposes (Used by NASA), Search Engines (Google), YouTube etc.  Features of Python Programming Language: i. The...

Ex: 5 - Coding in Python  Python has evolved into a very useful language as it is simple and is being used in many fields such as in Robotics, Scientific Purposes (Used by NASA), Search Engines (Google), YouTube etc.  Features of Python Programming Language: i. The keywords of Python are in simple English. ii. Python library is portable and compatible with all types of Operating. iii. Python is an interpreted language i.e. Python interprets and executes the code, line by line. iv. It can be easily integrated with C, C++, Java, etc.  IDLE is an acronym of Integrated Development Environment. The interactive interpreter of Python is called 'Python Shell'. It creates, debugs, runs and edits Python Programs 6rom a single interface.  There are two modes to work in Python: i. Interactive Mode ii. Script Mode  Interactive Mode It allows us to type a set of commands only in a line at the command prompt '>>>'. Python interprets the given command and gives the output when the 'Enter key’ is pressed for that line.  Script Mode In script mode, we type the set of codes in more than one line in a file and then use the interpreter to execute the complete program from the file.  Working in interactive mode is convenient for beginners and for testing small modules of code, as we can test them immediately for each line while in script mode, we can code a program with more number of lines and can also save, edit and reuse the code for future use.  Print' command is used to display the output on the screen i.e., 'Python Shell'.  If all the statements or commands are written in separate line then t is optional to put semicolon (;) at the end of each statement or command however it is compulsory to put semicolon (;) at the end of each statement on command when more than one commands or statements are written in the single line.  Keywords: Keywords are the reserved words that have specific function in a program. These words are used while writing program to get desired output. The keywords are executed as their purpose/action is already explained inside the language (interpreter). There are 33 keywords in Python. Some of them are: false, return, else, lambda, yield, from, def, import, none etc.  “.py” is the extension of the file saved in Python.  Identifiers: Identifiers are the fundamental building blocks of a program that are used to identify names of different elements of the program such as variable. These elements have specific properties that help in making program. Guidelines for Creating ldentifiers in Python: i. An identifier is an arbitrary sequence of letters in lower case (a to z), upper case (A to Z), digits (0 to 9) and underscore (). Page 1 of 4 ii. The first character of the identifier must be a letter (a to z or A to Z) or underscore (_). It must not begin with digits (0-9). iii. An identifier must not be a 'Keyword. iv. An identifier must not contain any special character except underscore (_).  Literals Literals are data items that never change their value during the execution of a program. In Python there are four types of literals (constants): Integer literals: These are whole numbers without any fractional part. It may be positive or negative. For example: 2, 5, 1324, -144 etc. Character literals: These refer to single character that is enclosed in single quote. For example: A,T etc. Floating literals : These are fractional number. They may be positive or negative. For example: 12.234, 10.453, -90,908 etc. String literal: It is a sequence of characters enclosed within pair of String literals single or double quotes. For example: MICA, Hello' etc.  In Python, There are three logical operators i.e., And, Or and not.  Operators Operators are tokens that do computation (calculation) with designated or given values in an expression. Operators when applied on operands form an expression. Operand is the value or variable on which calculation is performed. Operators can be classified as Unary, Arithmetic, Bitwise, Relational, Assignment, Logical etc.  VARIABLES Variables are the data or values which can change during the execution of a program. It is a name given to the location in memory in which the value is stored during the execution of a program. Rules for defining a Variable: i. A variable name may be the combination of alphabets, digits or underscore. Generally, the first character in the variable name is an alphabet. ii. Blank spaces cannot be used in a variable name. iii. Variable names are case sensitive. iv. The name of keywords cannot be used as the name of variables. v. A numeric variable stores a numeric value. It is a designated identity that stores numbers during the execution of program. vi. A string variable stores alphabets, special characters or their combination.  DATA TYPE These are the kind of data that are to be stored in the variables which is being used while writing a program. Python imagines the data type of a variable during the execution of program by their syntax. The interpreter of python automatically detects the type of data being entered. This is known as dynamic typing. There are five standard data types In Python: i. numbers ii. string iii. list iv. tuple Page 2 of 4 v. dictionary  Numbers This data type stores numeric values in the program. It is used for mathematical calculations. Syntax: variable name-value Example: marks=95 Here, marks' is the name of the number variable and ‘95' is the value.  Python supports different numerical types: a. int (integers): These are positive or negative whole number with no decimal point. For example: 10, -25 b. float (floating point real values): It consists of positive or negative numbers with decimal point. For example: 0.2, -1.4 c. complex: Complex are numbers that take the structure a + bJ where ‘a' and 'b’ are floating point numbers and that represent the square root of -1 (imaginary number).  string They are contiguous set of characters in between pairs of single or double quotes. The quotes are not a part of string, They only denote the beginning and end of the string. Syntax: String_name =”text” Example: name =”MICA” Here, 'name' is the name of the string variable and “MICA” is the text or string.  List It is a compound data type in which the items present in the list is separated by commas and enclosed within square brackets. Syntax: List_name =|Elements] Example: list= [‘MICA', 9, 1.2] Here, list' is the name of the list and [MICA, 9, 1.2] are the elements.  Tuple ‘Tuples' are similar to the list which consist of diferent values which are separated by commas and enclosed within parenthesis. Syntax: Tuple_name = (Elements) Example: tuplel = (MICA, 123, 1.10, 'Sanjay') Here, 'tuple' is the name of the tuple and (MICA, 123, 1.10, Sanjay') are the elements.  The main difference between lists and tuples is that lists are enclosed in square brackets and their elements and size can be changed, while tuples are enclosed in parentheses and cannot be updated once declared.  Dictionary These are kind of key value pairs. Key can be numbers or strings and values can be any arbitrary Python object. Dictionaries are enclosed by curly braces and values can be assigned and accessed using square brackets. Syntax: dictionary_name = {} dictionary_ name(key ]=Value Example: dict ={} dict['hello']= ‘Welcome to MICA' Page 3 of 4 Here, hello' is the name of the Key of dictionary variable and Welcome to MICA' is the Value.  Documentation Section The documentation section consists of a set of comment lines with the name of the program and other details relating to it. a. For Single Line Comment: Comment line always starts with the symbol #. Example: #My First Program b. For Multiple lines Comment: Comment should be enclosed between pair of triple apostrophe “’ “’. Example” ‘"My First Program’"  Declaration Section: Here, values can be assigned to variables and literals. The declaration occurs automatically when you assign a value to a variable or literal. The equal sign (=) is used to assign values to variables. The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator is the value stored in the variable. Example: marks=100 #Here, 'marks' is the name of variable and 100 is the value assigned to it.  Commands It is that portion of the program in which commands are written to make program and execute it. Syntax: print 'message’ [Or] print variable_name Example: print ‘Welcome to MICA’ Output: Welcome to MICA  print statement is used to display the output. Page 4 of 4

Use Quizgecko on...
Browser
Browser