Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction Python is an interpreted, object-oriented and high-level programming language, which is different from a compiled one like C/C++/Java. Its.

Similar presentations


Presentation on theme: "Introduction Python is an interpreted, object-oriented and high-level programming language, which is different from a compiled one like C/C++/Java. Its."— Presentation transcript:

1 Introduction Python is an interpreted, object-oriented and high-level programming language, which is different from a compiled one like C/C++/Java. Its code is concise but at the same highly readable (pythonic). It is dynamically typed: a variables does not need a predefined type. Other than the basic types, It also has built-in collection types: lists tuples dictionaries (maps) sets It can be linked to C/C++/Java codes that handle time-critical jobs more efficiently. It has a large collection of standard and special-purpose libraries.

2 Other Features It uses indentations (not braces or keywords) to define blocks. It has three frequently used sequence types: strings: ‘xyz’, immutable, for characters. lists: […], mutable, for any objects. tuples: (…), immutable, for any objects. It supports multiple paradigms (obj-oriented, functional, procedural). It has powerful subscripting/slicing methods. It is one of the most popular scripting languages (Perl, PHP, Tcl, JavaScript and Unix shell scripts). It has found numerous applications, especially in string/text manipulation, file/directory management, and web design. It is open-source and is available in all major systems.

3 Comparison C/C++/Java Statically typed.
A variable’s type must be declared before it can be used. Methods use type signatures to enforce contracts. Python Dynamically typed. A variables comes into existence when first assigned a value. A variable can refer to an object of any type. Most types are treated the same way. Drawback: type errors are caught at runtime only.

4 Python Versions Python 2.x The latest stable release: 2.7
It implements certain new features of 3.x and is fully backwards compatible. Python 3.x The latest release: 3.6.2 Strings use Unicode, not ASCII. It contains many new features and are cleaner. It has compatibility issues with the legacy codes written with 2.x versions.

5 Run Python interactively (command-line mode)
Python 2.5 (r25:51908, May , 16:14:04) [GCC (prerelease) (SUSE Linux)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> >>> 2+3 5 “>>>” is the Python prompt sign. “CTRL-D” to exit Python.

6 Run Python code in a file
% python example.py --- Add this line at the top of the file: #!/usr/bin/python --- Make the file executable. We will run Python in the IDLE environment (to be discussed in lab sessions).

7 A Code Sample # This is a Python code sample. a = 22 b = a + 33 c = d = "YSU" if a == 44 or d == "YSU": a = a +1 d = d + "Football" print (a) print (b) print (c) print (d)

8 Explanations Indentation has special meanings: It indicates a block structure. The first assignment to a variable creates it. A variable’s type does not need to be declared. Python figures out a variable type in the context. “=“ is assignment operator, and “==“ is comparison operator. “+”, “-”, “*”, “/”, “%” are arithmetic operators. “+” means concatenation for strings. “%” indicates formatting for a string (similar to “printf” in C). Logical operators are words (and, or, not). Simple I/O: num = input ('Enter a number: ') print (*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

9 Basic Types Integers (default for numbers) x = 34 Floating points
Strings s1 = “YSU” s2 = ‘YSU’

10 Whitespace and Indentation
Whitespace has special meanings in Python. Within an expression or a statement, whitespaces are ignored. Leading whitespace of a line defines indentation. Consistent indentations define blocks. The first line with less indentation is outside of the block. The first line with more indentation starts a nested block. A colon often appears at the start of a new block (function and class). A newline ends a line of code. Continuation of a long line to the next line is indicated by putting the backslash character (\) at the end of a line.

11 Assignments In Python, binding a variable means setting a name to hold a reference to an object. Assignment creates references, not copies (as in Java) A variable is created the first time it appears on the left side of an assignment expression: An object is deleted (by garbage collector) once it becomes unreachable. Names in Python do not have an intrinsic type. Objects have types. Python determines the type of a reference automatically based on what data is assigned to it.


Download ppt "Introduction Python is an interpreted, object-oriented and high-level programming language, which is different from a compiled one like C/C++/Java. Its."

Similar presentations


Ads by Google