Welcome to the blog !!! Let's begin our journey of learning python by understanding some simple basic concepts.
In this tutorial you’ll learn basics of the Python programming language. The first part covers variables, strings, lists.
The tutorial uses Python IDLE for Python development environment. If you have not installed Python yet take a look at Install Python 3.X On Windows – Python Installation Guide 💻
Let’s start with printing out our first statement - "Hello World"
Comments
If you’d like to include comments in your Python code you can use the #-sign. You can choose to add the the #-sign at the beginning of the line or to include the comment at the end of the line:
Variables
Using variables in Python is easy. You do not need to declare variables. Instead you can choose a name and assign the value directly as you can see in the following:
Now we can print our the value of var1 by using the following line of code:![]() |
As a result you’ll get the value: 2
In the following piece of code we’re defining a second variable with name var2 and a third variable with name var3. var3 gets assigned the sum of var1 and var2. Finally a print statement is used to print out the sum:
As a result the following text should be outputted:
Strings
In Python you can define strings by using single quotes or double quotes:
Of course, it’s possible to assign a string to a variable, e.g. string1:
Printing out the value of string1
In Python it’s possible to access single characters of the string by using brackets and passing in the index. As indexing always starts at 0 the following line of code:By adding a colon you’ll get back a substring starting from index 1 until the end of the string: Adding another number of the colon is returning a substring from index 5 to index 7 (not including):
Lists
Another important feature of the Python programming language are lists. You can define a list by using brackets and including the elements of the list separated by a comma:
Printing our the value of list1now:
A list can also include strings:If you want to append a list you can use the append method. The method is expecting to get the new element as a parameter. The following call of the append method is adding the element ‘d’ to the list which is stored in list2:Selecting a list element by using the index selector can be done in the following way:A re-assignment is also possible by using the index selector:It’s also possible to define nested lists. A nested list is a list which is included in another list. One way to create a nested list is to to execute the following line of code:Conclusion
I hope this tutorial helps you in learning Variables, Strings, Lists in Python