Ce diaporama a bien été signalé.
Le téléchargement de votre SlideShare est en cours. ×

Python 01.pptx

Prochain SlideShare
Python Programming 1.pptx
Python Programming 1.pptx
Chargement dans…3

Consultez-les par la suite

1 sur 60 Publicité

Plus De Contenu Connexe

Plus récents (20)


Python 01.pptx

  1. 1. Python https://www.programiz.com/python-programming/keywords- identifier
  2. 2. Python • Python is a cross-platform programming language, which means that it can run on multiple platforms like Windows, macOS, Linux, and has even been ported to the Java and .NET virtual machines. It is free and open-source. • Even though most of today's Linux and Mac have Python pre- installed in it, the version might be out-of-date. So, it is always a good idea to install the most current version.
  3. 3. The Easiest Way to Run Python • The Thonny IDE comes with the latest version of Python bundled in it. So you don't have to install Python separately. • Follow the following steps to run Python on your computer. 1. Download Thonny IDE. 2. Run the installer to install Thonny on your computer. • Go to: File > New. Then save the file with .py extension. For example, hello.py, example.py, etc. • You can give any name to the file. However, the file name should end with .py • Write Python code in the file and save it
  4. 4. Thonny Python IDE
  5. 5. Features • Easy to get started. Thonny comes with Python 3.10 built in, so just one simple installer is needed and you're ready to learn programming. (You can also use a separate Python installation, if necessary.) The initial user interface is stripped of all features that may distract beginners.
  6. 6. continue • Simple debugger. Just press Ctrl+F5 instead of F5 and you can run your programs step-by- step, no breakpoints needed. Press F6 for a big step and F7 for a small step. Steps follow program structure, not just code lines. • Step through expression evaluation. If you use small steps, then you can even see how Python evaluates your expressions. You can think of this light-blue box as a piece of paper where Python replaces subexpressions with their values, piece-by-piece.
  7. 7. continue • Highlights syntax errors. Unclosed quotes and parentheses are the most common beginners' syntax errors. Thonny's editor makes these easy to spot. • Code completion. Students can explore APIs with the help of code completion.
  8. 8. Python Keywords and Identifiers • Keywords are predefined, reserved words used in Python programming that have special meanings to the compiler. • We cannot use a keyword as a variable name, function name, or any other identifier. They are used to define the syntax and structure of the Python language. • All the keywords except True, False and None are in lowercase and they must be written as they are. The list of all the keywords is given below.
  9. 9. Python Keywords List
  10. 10. Rules for Naming an Identifier • Identifiers cannot be a keyword. • Identifiers are case-sensitive. • It can have a sequence of letters and digits. However, it must begin with a letter or _. The first letter of an identifier cannot be a digit. • It's a convention to start an identifier with a letter rather • Whitespaces are not allowed. • We cannot use special symbols like !, @, #, $, and so on.
  11. 11. Things to Remember • Python is a case-sensitive language. This means, Variable and variable are not the same. • Multiple words can be separated using an underscore, like this_is_a_long_variable.
  12. 12. Python Comments
  13. 13. Python Comments • In computer programming, comments are hints that we use to make our code more understandable. • Comments are completely ignored by the interpreter. They are meant for fellow programmers. For example,
  14. 14. Types of Comments in Python • In Python, there are two types of comments: • single-line comment • multi-line comment • Multi-line Comment in Python • Python doesn't offer a separate way to write multiline comments. • We can use # at the beginning of each line of comment on multiple lines
  15. 15. Python Variables, Constants and Literals
  16. 16. Python Variables • In programming, a variable is a container (storage area) to hold data. For example,
  17. 17. Changing the Value of a Variable in Python
  18. 18. Assigning multiple values to multiple variables
  19. 19. Python Constants
  20. 20. Python Literals • Literals are representations of fixed values in a program. They can be numbers, characters, or strings, etc. For example, 'Hello, World!', 12, 23.0, 'C', etc.
  21. 21. Python Data Types • In computer programming, data types specify the type of data that can be stored inside a variable. For example,
  22. 22. Python List Data Type • List is an ordered collection of similar or different types of items separated by commas and enclosed within brackets [ ]. For example, • Here, we have created a list named languages with 3 string values inside it.
  23. 23. Access List Items • To access items from a list, we use the index number (0, 1, 2 ...). For example,
  24. 24. Python Tuple Data Type • Tuple is an ordered sequence of items same as a list. The only difference is that tuples are immutable. Tuples once created cannot be modified. • In Python, we use the parentheses () to store items of a tuple. For example,
  25. 25. Access Tuple Items • Similar to lists, we use the index number to access tuple items in Python . For example.
  26. 26. Python String Data Type
  27. 27. Python Set Data Type • Set is an unordered collection of unique items. Set is defined by values separated by commas inside braces { }. For example,
  28. 28. Python Type Conversion • In programming, type conversion is the process of converting data of one type to another. For example: converting integer data to string. • There are two types of type conversion in Python. • Implicit Conversion - automatic type conversion • Explicit Conversion - manual type conversion
  29. 29. Python Implicit Type Conversion • Python automatically converts one data type to another. This is known as implicit type conversion. • Example 1: Converting integer to float
  30. 30. Explicit Type Conversion • In Explicit Type Conversion, users convert the data type of an object to required data type. • We use the built-in functions like int(), float(), str(), etc to perform explicit type conversion. • This type of conversion is also called typecasting because the user casts (changes) the data type of the objects.
  31. 31. Example 2: Addition of string and integer Using Explicit Conversion
  32. 32. Key Points to Remember • Type Conversion is the conversion of an object from one data type to another data type. • Implicit Type Conversion is automatically performed by the Python interpreter. • Python avoids the loss of data in Implicit Type Conversion. • Explicit Type Conversion is also called Type Casting, the data types of objects are converted using predefined functions by the user. • In Type Casting, loss of data may occur as we enforce the object to a specific data type.
  33. 33. Python Basic Input and Output • Python Output • In Python, we can simply use the print() function to print output. For example,
  34. 34. Syntax of print() • object - value(s) to be printed • sep (optional) - allows us to separate multiple objects inside print(). • end (optional) - allows us to add add specific values like new line "n", tab "t" • file (optional) - where the values are printed. It's default value is sys.stdout (screen) • flush (optional) - boolean specifying if the output is flushed or buffered. Default: False
  35. 35. Example 2: Python print() with end Parameter
  36. 36. Example 3: Python print() with sep parameter
  37. 37. Python input & output • While programming, we might want to take the input from the user. In python, we can use the input() function.
  38. 38. Example: Print Python Variables and Literals
  39. 39. Example: Print Concatenated Strings
  40. 40. Python Input • While programming, we might want to take the input from the user. In Python, we can use the input() function. • Syntax of input()
  41. 41. Example: Python User Input
  42. 42. Python Operators
  43. 43. Types of Python Operators 1. Arithmetic operators 2. Assignment Operators 3. Comparison Operators 4. Logical Operators 5. Bitwise Operators 6. Special Operators
  44. 44. 1. Python Arithmetic Operators
  45. 45. Example 1: Arithmetic Operators in Python
  46. 46. 2. Python Assignment Operators • Assignment operators are used to assign values to variables. For example,
  47. 47. Example 2: Assignment Operators
  48. 48. 3. Python Comparison Operators
  49. 49. Example 3: Comparison Operators
  50. 50. 4. Python Logical Operators
  51. 51. Python Namespace And Scope
  52. 52. What is Name in Python? • Name (also called identifier) is simply a name given to objects. Everything in Python is an object. Name is a way to access the underlying object. • For example, when we do the assignment a = 2, 2 is an object stored in memory and a is the name we associate it with. We can get the address (in RAM) of some object through the built-in function id(). Let's look at how to use it.
  53. 53. Name in Python?
  54. 54. What is a Namespace in Python? • To simply put it, a namespace is a collection of names. • In Python, you can imagine a namespace as a mapping of every name you have defined to corresponding objects. • Different namespaces can co-exist at a given time but are completely isolated. • A namespace containing all the built-in names is created when we start the Python interpreter and exists as long as the interpreter runs. • This is the reason that built-in functions like id(), print() etc. are always available to us from any part of the program. Each module creates its own global namespace.
  55. 55. Python Variable Scope • A scope is the portion of a program from where a namespace can be accessed directly without any prefix. • At any given moment, there are at least three nested scopes. 1. Scope of the current function which has local names 2. Scope of the module which has global names 3. Outermost scope which has built-in names
  56. 56. Python Variable Scope • When a reference is made inside a function, the name is searched in the local namespace, then in the global namespace and finally in the built-in namespace. • If there is a function inside another function, a new scope is nested inside the local scope.
  57. 57. Example of Scope and Namespace in Python • Here, the variable a is in the global namespace. Variable b is in the local namespace of outer_function() and c is in the nested local namespace of inner_function(). • When we are in inner_function(), c is local to us, b is nonlocal and a is global. We can read as well as assign new values to c but can only read b and a from inner_function().