Welcome
Welcome to the LearnPython.org interactive Python tutorial.
Learn the basics of the world's fastest growing and most popular programming language used by software engineers, analysts, data scientists, and machine learning engineers alike.
Python is considered one of the easiest programming languages to learn. While anyone can learn Python programming — even if you've never written a line of Python code before — you should expect that it will take time, and you should expect moments of frustration.
Whether you are an experienced programmer or not, this website is intended for everyone who wishes to learn the Python programming language.
Just click on the chapter you wish to begin from, and follow the instructions. Good luck!
Learn the Basics
1: Hello! World!
Hello! World!(Program)
Python is a very simple language, and has a very straightforward syntax. It encourages programmers to program without boilerplate (prepared) code. The simplest directive in Python is the "print" directive - it simply prints out a line (and also includes a newline, unlike in C).
There are two major Python versions, Python 2 and Python 3. Python 2 and 3 are quite different. This tutorial uses Python 3, because it more semantically correct and supports newer features.
A simple program that displays “Hello, World!”. It's often used to illustrate the syntax of the language.
2: Variables and Types
Variables and Types(Program)
Python is completely object oriented, and not "statically typed". You do not need to declare variables before using them, or declare their type. Every variable in Python is an object.
For example: >>> >>> n = 300.
- Numbers
Python supports two types of numbers - integers(whole numbers) and floating point numbers(decimals). (It also supports complex numbers, which will not be explained in this tutorial).
- Strings
Strings are defined either with a single quote or a double quotes.
Python String Concatenation and Variable.
For example, we will concatenate “Guru” with the number “99”. Once the integer is declared as string, it can concatenate both “Guru” + str(“99”)= “Guru99” in the output.
3: - Lists
Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
For example, you want to create a list to store the first five even natural numbers. Even= [2,4,6,8,10]. Here [2,4,6,8,10] are the list items of the list named Even.
Exercise(DO)
The target of this exercise is to create a string, an integer, and a floating point number. The string should be named mystring and should contain the word "hello". The target of this exercise is to create a string, an integer, and a floating point number.
In this exercise, you will need to add numbers and strings to the correct lists using the "append" list method. You must add the numbers 1,2, and 3 to the "numbers" list, and the words 'hello' and 'world' to the strings variable.
4: - Basic Operators
Basic Operator(Program)
This section explains how to use basic operators in Python.
Arithmetic Operators
- Addition.
- Subtraction.
- Multiplication.
- Division.
- Modulus.
- Exponentiation.
- Floor division
Using Operators with Strings
- Assignment operator: “=.”
- Concatenate operator: “+.”
- String repetition operator: “*.”
- String slicing operator: “[]”
- String comparison operator: “==” & “!= ”
- Membership operator: “in” & “not in”
- Escape sequence operator: “\.”
- String formatting operator: “%” & “{}”
Using Operators with Lists
append() The append() method is used to add elements at the end of the list.extend() The extend() method is used to add more than one element at the end of the list.
insert() The insert() method can add an element at a given position in the list.
- remove()
- pop()
- slice.
- reverse()
- len()
To print list elements separated by spaces without brackets [] first we convert the list to a string by passing the str and list as arguments to the map () function.
The in operator determines whether a given value is a constituent element of a sequence such as a string, array, list, or tuple. When used in a condition, the statement returns a Boolean result of True or False.
Exercise
The target of this exercise is to create two lists called x_list
and y_list
, which contain 10 instances of the variables x
and y
, respectively. You are also required to create a list called big_list
, which contains the variables x
and y
, 10 times each, by concatenating the two lists you have created.
5: - String Formatting
String Formatting(Program)
formatted string literals are a Python parser feature that converts f-strings into a series of string constants and expressions. They then get joined up to build the final string.
string formatting uses a process of string interpolation (variable substitution) to evaluate a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.
Python uses C-style string formatting to create new, formatted strings. The "%" operator is used to format a set of variables enclosed in a "tuple" (a fixed size list), together with a format string, which contains normal text together with "argument specifiers", special symbols like "%s" and "%d".
The Format String is the argument of the Format Function and is an ASCII Z string which contains text and format parameters, like: printf (“The magic number is: %d\n”, 1911); • The Format String Parameter, like %x %s defines the type of conversion of the format function.
For Example: -
String formatting is also known as String interpolation. It is the process of inserting a custom string or variable in predefined text. As a data scientist, you would use it for inserting a title in a graph, show a message or an error, or pass a statement to a function.
6: - Basic String Operations
Basic String Operations(Program)
- Assignment operator: “=.”
- Concatenate operator: “+.”
- String repetition operator: “*.”
- String slicing operator: “[]”
- String comparison operator: “==” & “!= ”
- Membership operator: “in” & “not in”
- Escape sequence operator: “\.”
Method | Description |
---|---|
rstrip() | Returns a right trim version of the string |
split() | Splits the string at the specified separator, and returns a list |
splitlines() | Splits the string at line breaks and returns a list |
startswith() | Returns true if the string starts with the specified value |
The first thing you learned was printing a simple sentence. This sentence was stored by Python as a string. However, instead of immediately printing strings out, we will explore the various things you can do to them. You can also use single quotes to assign a string. However, you will face problems if the value to be assigned itself contains single quotes
7: - Conditions
Conditions(Program)
Python supports the usual logical conditions from mathematics:
Python Conditions and If statements
Python condition & if statement(Program)
Elif
Else
Short Hand If
Short Hand If ..... Else
And
Or
Nested If
The pass Statement
8: - Loops
Loops(Program)
There are two types of loops in Python, for and while.
The "for" loop
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.
For loops can iterate over a sequence of numbers using the "range" and "xrange" functions. The difference between range and xrange is that the range function returns a new list with numbers of that specified range, whereas xrange returns an iterator, which is more efficient. (Python 3 uses the range function, which acts like xrange). Note that the range function is zero based
Syntax:
for iterator_var in sequence: statements(s)
Output:
List Iteration
geeks
for
geeks
Tuple Iteration
geeks
for
geeks
String Iteration
G
e
e
k
s
Dictionary Iteration
xyz 123
"while" loops
Python While Loop is used to execute a block of statements repeatedly until a given condition is satisfied. And when the condition becomes false, the line immediately after the loop in the program is executed.
While loops repeat as long as a certain boolean condition is met.
Syntax:
while expression: statement(s)
Example of Python while Loop