cover photo

RESOURCE · 2/4/2023

Basics of python

Python is a widely used high-level programming language that is known for its simplicity, readability, and versatility. It can be used for a wide range of tasks, including web development, data analys

Aryan Aithal
Aryan Aithal
OP
Basics of python
This Article is yet to be approved by a Coordinator.

Basics\n### How to take input from user? \nFor this we use input function and assign the vanlue to the appropriate variable. The return type of the input is string i.e whatever the input given by the user is stored in the variable in the form of string.\n\n\npython\nvariable1 = input(\Type the input: \")\ntype(variable1)\n\n\n Type the input: 69\n str\n\n\n\nIn above example, we used input function and assigned the value to "variable1". \nInside input function we use a string ["Type the input: "] that would be the prompt to the terminal i.e the message to which the user would respond with an input.\n\nNow to convert the input to required data type we use type casting.\ni.e int() -> for integer, \nfloat() -> for float, \nchr() -> for a character, \nstr() -> for string [can be used to convert other data type to string form].\n\n\npython\nvariable1 = int(float(input(\"Enter the number: \")))\nprint(variable1)\n\n\n Enter the number: 69.96\n 69\n \n\nIn The above example we used input function which gives the input in the form of string, then converted it to floating value, then to int. In the last step the decimal value is discarded. Note that the string can be directly converted to integer.\n\nFor output we use print function \n\nWe could cluster and print multiple things separated by a comma )\n\n\npython\nprint(\"The intiger value of the number is\" variable \"which is not\", 8.85)\n\n\n The intiger value of the number is 69 which is not 8.85\n \n\nNote that the print function adds a space in between each element being printed\n\n### Lists\n\nlist is a builtin datatype in python. It is used to store multiple data in a single variable. It is similar to array. The elements of lists are stored in [ ] . To create an empty list...\n\n\npython\nlist_name = []\n\n\n\npython\n# A list with 3 integers\nnumbers = [1, 2, 5]\n\nprint(numbers)\n\n# Output: [1, 2, 5] \n\n\n [1, 2, 5]\n \n\n\npython\n# list with mixed data types\nmy_list = [1, \"Hello\", 3.4]\n\n\nTo create a list of size n we cant copy the same as c++ or c. Insted we do this... where inside range is the number size of the list.\n\n\npython\nmy_list=list(range(5))\nprint(my_list)\n\n\n [0, 1, 2, 3, 4]\n \n\nIn Python, each item in a list is associated with a number. The number is known as a list index.\n\nWe can access elements of an array using the index number (0, 1, 2 …). For exampl \n\n\npython\nlanguages = [\"Python\", \"Swift\", \"C++\"]\n\n# access item at index 0\nprint(languages[0]) # Python\n\n# access item at index 2\nprint(languages[2]) # C++\n\n\n Python\n C++\n \n\n#### Negative Indexing in Python\nPython allows negative indexing for its sequences. The index of -1 refers to the last item, -2 to the second last item and so on.\n\nLet's see an exampl \n\n\npython\nlanguages = [\"Python\", \"Swift\", \"C++\"]\n\n# access last item \nprint(languages[-1]) # C++\n\n\n C++\n \n\n#### Add Elements to a Python List\nPython List provides different methods to add items to a list.\n\n##### 1. Using append()\n\nThe append() method adds an item at the end of the list. For exampl \n\n\npython\nnumbers = [21, 34, 54, 12]\n\nprint(\"Before Append:\", numbers)\n\n# using append method\nnumbers.append(32)\n\nprint(\"After Append:\", numbers)\n\n\n Before Append: [21, 34, 54, 12]\n After Append: [21, 34, 54, 12, 32]\n \n\n##### 2. Using extend()\n\nWe use the extend() method to add all items of one list to another. For exampl \n\n\npython\nprime_numbers = [2, 3, 5]\nprint(\"List1:\", prime_numbers)\n\neven_numbers = [4, 6, 8]\nprint(\"List2:\", even_numbers)\n\n# join two lists\nprime_numbers.extend(even_numbers)\n\nprint(\"List after append:\", prime_numbers) \n\n\n List1: [2, 3, 5]\n List2: [4, 6, 8]\n List after append: [2, 3, 5, 4, 6, 8]\n \n\n#### Change List Items\nPython lists are mutable. Meaning lists are changeable. And, we can change items of a list by assigning new values using = operator. For exampl \n\n\npython\nlanguages = [\'Python\', \'Swift\', \'C++\']\n\n# changing the third item to \'C\'\nlanguages[2] = \'C\'\n\nprint(languages) # [\'Python\', \'Swift\', \'C\']\n\n\n ['Python', 'Swift', 'C']\n \n\n#### Remove an Item From a List\n##### 1. Using del()\n\nIn Python we can use the del statement to remove one or more items from a list. For exampl \n\n\npython\nlanguages = [\'Python\', \'Swift\', \'C++\', \'C\', \'Java\', \'Rust\', \'R\']\n\n# deleting the second item\ndel languages[1]\nprint(languages) # [\'Python\', \'C++\', \'C\', \'Java\', \'Rust\', \'R\']\n\n# deleting the last item\ndel languages[-1]\nprint(languages) # [\'Python\', \'C++\', \'C\', \'Java\', \'Rust\']\n\n# delete first two items\ndel languages[0 : 2] # [\'C\', \'Java\', \'Rust\']\nprint(languages)\n\n\n ['Python', 'C++', 'C', 'Java', 'Rust', 'R']\n ['Python', 'C++', 'C', 'Java', 'Rust']\n ['C', 'Java', 'Rust']\n \n\n##### 2. Using remove()\n\nWe can also use the remove() method to delete a list item. For exampl \n\n\npython\nlanguages = [\'Python\', \'Swift\', \'C++\', \'C\', \'Java\', \'Rust\', \'R\']\n\n# remove \'Python\' from the list\nlanguages.remove(\'Python\')\n\nprint(languages) # [\'Swift\', \'C++\', \'C\', \'Java\', \'Rust\', \'R\']\n\n\n ['Swift', 'C++', 'C', 'Java', 'Rust', 'R']\n\nFor futher learning i reccomend this course 100-days-of-code"

UVCE,
K. R Circle,
Bengaluru 01