diff --git a/python_data_structures.md b/python_data_structures.md index 67aeeff..0638252 100644 --- a/python_data_structures.md +++ b/python_data_structures.md @@ -1,5 +1,85 @@ # Data Structures in Python Data Structures are a way of organizing data so that it can be accessed more efficiently depending upon the situation. Data Structures are fundamentals of any programming language around which a program is built. +## Integer: + +Python Integer is a whole number, positive or negative, without decimals, of unlimited length. + + +### Python Strings Operations +```python +# Integers can be binary, octal, and hexadecimal values. +>>> 0b11011000 # binary +216 +>>> 0o12 # octal +10 +>>> 0x12 # hexadecimal +18 + +# Use the type() method to get the class name +>>>type(100) + # type is int +>>>x = 87 +>>>type(87) + # type of x is int + +# Leading zeros in non-zero integers are not allowed +>>>01234567890 +SyntaxError: leading zeros in decimal integer literals are not permitted; + +# But 00000 is 0 +>>>000000000 +0 +``` +### Operations on Integers + +```python +>>> 100 +100 +# addition +>>> 12 + 18 +30 +# subtraction +>>>10 - 5 +5 +# Multiplication +>>> 3*4 +12 +# Division with Quotient in decimals +>>> 5/2 +2.5 +# Division to get Quotient as whole number +>>> 5//2 +2 +# Division to get Remainder +>>> 5%2 +1 +# Division By 0 +>>> 8/0 +Traceback (most recent call last): + File "", line 1, in +ZeroDivisionError: division by zero +``` + +## String: + +Python Strings are arrays of bytes representing Unicode characters. In simpler terms, a string is an immutable array of characters. Python does not have a character data type, a single character is simply a string with a length of 1. + +Note: As strings are immutable, modifying a string will result in creating a new copy. + +### Python Strings Operations +```python +String = "Welcome to Workshop" +print("Creating String: ") +print(String) + +# Printing First character +print("\nFirst character of String is: ") +print(String[0]) + +# Printing Last character +print("\nLast character of String is: ") +print(String[-1]) +``` ## Lists: Python Lists are just like the arrays, declared in other languages which is an ordered collection of data. It is very flexible as the items in a list do not need to be of the same type. @@ -16,10 +96,15 @@ List elements can be accessed by the assigned index. In python starting index of ```python # Creating a List with -# the use of multiple values +# the use of multiple values List = ["Python", "Git", "Docker"] print("\nList containing multiple values: ") print(List) + +# Why is it called list and not array? +print(id(List[0])) # address of "Python" +print(id(List[1])) # address of "Git" +print(id(List[2])) # address of "Docker" # Creating a Multi-Dimensional List # (By Nesting a list inside a List) @@ -41,32 +126,6 @@ print(List[-1]) # print the last element of list # print the third last element of list print(List[-3]) ``` -## Dictionary: - -Python dictionary is like hash tables in any other language with the time complexity of O(1). It is an unordered collection of data values, used to store data values like a map, which, unlike other Data Types that hold only a single value as an element, Dictionary holds the key:value pair. Key-value is provided in the dictionary to make it more optimized. - -Indexing of Python Dictionary is done with the help of keys. These are of any hashable type i.e. an object whose can never change like strings, numbers, tuples, etc. We can create a dictionary by using curly braces ({}) or dictionary comprehension. - -### Python Dictionary Operations - -```python -# Creating a Dictionary -Dict = {'Name': 'Python', 1: [1, 2, 3, 4]} -print("Creating Dictionary: ") -print(Dict) - -# Accessing a element using key -print("Accessing a element using key:") -print(Dict['Name']) - -# Accessing a element using get() method -print("Accessing a element using get:") -print(Dict.get(1)) - -# Creation using Dictionary comprehension -myDict = {x: x**2 for x in [1,2,3,4,5]} -print(myDict) -``` ## Tuple: Python Tuple is a collection of Python objects much like a list but Tuples are immutable in nature i.e. the elements in the tuple cannot be added or removed once created. Just like a List, a Tuple can also contain elements of various types. @@ -98,6 +157,44 @@ print(Tuple[-1]) print("\nThird last element of tuple") print(Tuple[-3]) ``` + +### List and Tuple comparison +```python +# tuples are memory efficient +import sys +List = ["Python", "Git", "Docker"] +Tuple = ("Python", "Git", "Docker") +print(sys.getsizeof(List)) +print(sys.getsizeof(Tuple)) +# +``` + +## Dictionary: + +Python dictionary is like hash tables in any other language with the time complexity of O(1). It is an unordered collection of data values, used to store data values like a map, which, unlike other Data Types that hold only a single value as an element, Dictionary holds the key:value pair. Key-value is provided in the dictionary to make it more optimized. + +Indexing of Python Dictionary is done with the help of keys. These are of any hashable type i.e. an object whose can never change like strings, numbers, tuples, etc. We can create a dictionary by using curly braces ({}) or dictionary comprehension. + +### Python Dictionary Operations + +```python +# Creating a Dictionary +Dict = {'Name': 'Python', 1: [1, 2, 3, 4]} +print("Creating Dictionary: ") +print(Dict) + +# Accessing a element using key +print("Accessing a element using key:") +print(Dict['Name']) + +# Accessing a element using get() method +print("Accessing a element using get:") +print(Dict.get(1)) + +# Creation using Dictionary comprehension +myDict = {x: x**2 for x in [1,2,3,4,5]} +print(myDict) +``` ## Set: Python Set is an ordered collection of data that is mutable and does not allow any duplicate element. Sets are basically used to include membership testing and eliminating duplicate entries. The data structure used in this is Hashing, a popular technique to perform insertion, deletion, and traversal in O(1) on average. @@ -127,26 +224,6 @@ print() # Checking the element using in keyword print("Python" in Set) ``` -## String: - -Python Strings are arrays of bytes representing Unicode characters. In simpler terms, a string is an immutable array of characters. Python does not have a character data type, a single character is simply a string with a length of 1. - -Note: As strings are immutable, modifying a string will result in creating a new copy. - -### Python Strings Operations -```python -String = "Welcome to Workshop" -print("Creating String: ") -print(String) - -# Printing First character -print("\nFirst character of String is: ") -print(String[0]) - -# Printing Last character -print("\nLast character of String is: ") -print(String[-1]) -``` These are some basic python data structures. However, other than these there are a number of other data structures that are present in python. diff --git a/python_intro.md b/python_intro.md index 4d5e596..d6cb95e 100644 --- a/python_intro.md +++ b/python_intro.md @@ -27,7 +27,6 @@ Following are important characteristics of python − - It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java. -## Applications of Python As mentioned before, Python is one of the most widely used language over the web. I'm going to list few of them here: - Easy-to-learn − Python has few keywords, simple structure, and a clearly defined syntax. This allows the student to pick up the language quickly. @@ -65,19 +64,30 @@ Meanwhile, Python 3.0 was released in 2008. Python 3 is not backward compatible # How is python different from other programming languages: +Comparison Between Cpp, Java and Python + +| C++ | JAVA | PYTHON | +|-------------------------------------------------------|--------------------------------------------------------|--------------------------------------------------------------------------| +| Compiled Programming language | Compiled Programming language | Interpreted Programming Language | +| Provide both single and multiple inheritance | Provide partial multiple inheritance using interfaces | Provide both single and multiple inheritance | +| Platform dependent | Platform Independent | Platform Independent | +| Has limited number of library support | Has library support for many concepts like UI | Has a huge set of libraries that make it fit for AI, datascience, etc | +| Code length is a bit lesser, 1.5 times less that java | Java has quite huge code | Smaller code length, 3-4 times less than java. | +| Functions and variables are used outside the class | Every bit of code is inside a class | Functions and variables can be declared and used outside the class also. | +| C++ program is fast compiling | Java Program compiler a bit slower than C++ | Due to the use of interpreter execution is slower. | | + # Running Python -Get the App from git -```shell -https://github.com/UniCourt/Analytics-Workshop2 -``` -cd into the app folder -Use this command to start a python container. Use two separate terminals to run these commands -```shell -docker-compose up -``` +Run docker ps command and check for a python image available locally ```shell -docker exec -it python-container sh +docker images |grep "python" +python 3.7 7d2ecbd72983 7 months ago 907MB + +docker run -it python:3.7 +Python 3.7.14 (default, Sep 13 2022, 15:16:28) +[GCC 10.2.1 20210110] on linux +Type "help", "copyright", "credits" or "license" for more information. +>>> ``` # There are two different ways to start Python: