Ch-2 Python Introduction PDF
Document Details
Uploaded by ProlificPorcupine4283
CUNY John Jay College of Criminal Justice
Tags
Summary
This document is an introduction to Python programming concepts. It covers the fundamentals of Python variables, data types, operators and Python related topics. It also provides details on various concepts of Python programming.
Full Transcript
CH 2 Python Introduction Learning Outcomes After actively interacting with the content, you will be able to: CO2:Develop python programs to solve simple problems. Introduction to Python Advantages of python Easy Language...
CH 2 Python Introduction Learning Outcomes After actively interacting with the content, you will be able to: CO2:Develop python programs to solve simple problems. Introduction to Python Advantages of python Easy Language Readable Interpreted Language Dynamically-Typed Language Object-Oriented Open-Source Large Standard Library Platform-Independent GUI Support High level Language 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. Application of Python 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. What is Interpreter? Python Editors Python print() Function Basic Structure Of Python Program 1. 2. 3. 4. 5. 6. Basic Structure Keywords Python has a set of keywords that are reserved words that cannot be used as variable names, function names, or any other identifiers. Keywords may differ as per version of python. Sometimes they add, delete or modify them in the newer versions. Keyword Description and A logical operator as To create an alias assert For debugging break To break out of a loop class To define a class continue To continue to the next iteration of a loop def To define a function del To delete an object elif Used in conditional statements, same as else if else Used in conditional statements except Used with exceptions, what to do when an exception occurs False Boolean value, result of comparison operations finally Used with exceptions, a block of code that will be executed no matter if there is an exception or not for To create a for loop from To import specific parts of a module global To declare a global variable if To make a conditional statement Keyword Description import To import a module in To check if a value is present in a list, tuple, etc. is To test if two variables are equal lambda To create an anonymous function None Represents a null value nonlocal To declare a non-local variable not A logical operator or A logical operator pass A null statement, a statement that will do nothing raise To raise an exception Return To exit a function and return a value True Boolean value, result of comparison operations try To make a try...except statement while To create a while loop with Used to simplify exception handling yield To end a function, returns a generator Python Identifiers Python Identifier is the name we give to identify a variable, function, class, module or other object. That means whenever we want to give an entity a name, that’s called identifier. 1. A keyword cannot be used as an identifier. 2. An identifier can be in lowercase, uppercase, digits, or the combination of all three. Underscore (_) is also valid. 3. An identifier cannot begin with a digit. The first character should be an alphabet (uppercase or lowercase) or an underscore. 4. Symbols such as #, $, %, @, etc cannot be used in identifiers 5. There is no length limit for identifiers in Python. E X A M P L E O F VA L I D I D E N T I F I E R I N E X A M P L E S O F I N VA L I D I D E N T I F I E R I N PYTHON PYTHON variable1 1variable MyVariable 2Class _variable variable$ Class1 function## class2 function1 Function2 Variables Variables are containers for storing data values. We store data in computer memory through the variable. Creating Variables Python has no command for declaring a variable. A variable is created the moment you first assign a value to it. 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) A variable name cannot be any of the Python keywords. LEGAL VARIABLE NAMES: ILLEGAL VARIABLE NAMES: myvar = "John" 2myvar = "John" my_var = "John" my-var = "John" _my_var = "John" my var = "John" myVar = "John" MYVAR = "John" myvar2 = "John" Declaration of variable 1. 2.. Assigning single Value 4. Reassigning the variable 5. Deleting Variable 6. Take value from the user, input() function Python Data Types Python Data Types Mutable Data Types Data types in python where the value assigned to a variable can be changed are called mutable data types. Immutable Data Types Data types in python where the value assigned to a variable cannot be changed are called immutable data types. Numeric Data Type in Python Sequence Data Type in Python Python String Python List Python Tuple String Create a string List Tuple Creating Tuples Range Dictionary Creating Dictionaries Example Boolean x = True #display x: print(x) #display the data type of x: print(type(x)) Output: True Set Creating set Python - Operators Python operators are the constructs which can manipulate the value of operands. These are symbols used for the purpose of logical, arithmetic and various other operations. Consider the expression 4 + 5 = 9. Here, 4 and 5 are called operands and + is called operator. In this tutorial, we will study different types of Python operators. Types of Python Operators Arithmetic Operators Comparison (Relational) Operators Assignment Operators Logical Operators Bitwise Operators Membership Operators Identity Operators Python Arithmetic Operators Python arithmetic operators are used to perform mathematical operations on numerical values. These operations are Addition, Subtraction, Multiplication, Division, Modulus, Expoents and Floor Division. Operator Operation Example + Addition 5+2=7 - Subtraction 4-2=2 * Multiplication 2*3=6 / Division 4/2=2 // Floor Division 10 // 3 = 3 % Modulo 5%2=1 ** Power 4 ** 2 = 16 Python Comparison Operators Operator Meaning Example == Is Equal To 3 == 5 gives us False != Not Equal To 3 != 5 gives us True > Greater Than 3 > 5 gives us False < Less Than 3 < 5 gives us True Greater Than or Equal >= 3 >= 5 give us False To