python-1-chapter
python-1-chapter
Python
Python is a dynamic, high-level, free open source, and interpreted programming
language. It supports object-oriented programming as well as procedural-oriented
programming. In Python, we don’t need to declare the type of variable because it is a
dynamically typed language. For example, x = 10 Here, x can be anything such as
String, int, etc. In this article we will see what characteristics describe the python
programming language
Features in Python
In this section we will see what are the features of Python programming language:
1. Free and Open Source
Python language is freely available at the official website and you can download it
from the given download link below click on the Download
Python keyword. Download Python Since it is open-source, this means that source
code is also available to the public. So you can download it, use it as well as share it.
2. Easy to code
Python is a high-level programming language. Python is very easy to learn the
language as compared to other languages like C, C#, Javascript, Java, etc. It is very
easy to code in the Python language and anybody can learn Python basics in a few
hours or days. It is also a developer-friendly language.
3. Easy to Read
As you will see, learning Python is quite simple. As was already established, Python’s
syntax is really straightforward. The code block is defined by the indentations rather
than by semicolons or brackets.
4. Object-Oriented Language
One of the key features of Python is Object-Oriented programming. Python supports
object-oriented language and concepts of classes, object encapsulation, etc.
5. GUI Programming Support
Graphical User interfaces can be made using a module such as PyQt5, PyQt4,
wxPython, or Tk in Python. PyQt5 is the most popular option for creating graphical
apps with Python.
6. High-Level Language
Python is a high-level language. When we write programs in Python, we do not need
to remember the system architecture, nor do we need to manage the memory.
7. Large Community Support
Python has gained popularity over the years. Our questions are constantly answered
by the enormous StackOverflow community. These websites have already provided
answers to many questions about Python, so Python users can consult them as needed.
8. Easy to Debug
Excellent information for mistake tracing. You will be able to quickly identify and
correct the majority of your program’s issues once you understand how
to interpret Python’s error traces. Simply by glancing at the code, you can determine
what it is designed to perform.
9. Python is a Portable language
Python language is also a portable language. For example, if we have Python code for
Windows and if we want to run this code on other platforms such as Linux, Unix, and
Mac then we do not need to change it, we can run this code on any platform.
10. Python is an Integrated language
Python is also an Integrated language because we can easily integrate Python with
other languages like C, C++, etc
Keywords in Python
Python Keywords are some predefined and reserved words in Python that
have special meanings. Keywords are used to define the syntax of the
coding. The keyword cannot be used as an identifier, function, or variable
name. All the keywords in Python are written in lowercase except True and
False. There are 35 keywords in Python 3.11.
In Python, there is an inbuilt keyword module that provides an iskeyword()
function that can be used to check whether a given string is a valid keyword
or not. Furthermore, we can check the name of the keywords in Python by
using the kwlist attribute of the keyword module.
Else is used with if and elif conditional statements. The else block
else
is executed if the given condition is not true.
The following code allows you to view the complete list of Python‟s
keywords.
This code imports the “keyword” module in Python and then prints a list of all
the keywords in Python using the “kwlist” attribute of the “keyword” module.
The “kwlist” attribute is a list of strings, where each string represents a
keyword in Python. By printing this list, we can see all the keywords that are
1
Identifiers in Python
Identifier is a user-defined name given to a variable, function, class, module,
etc. The identifier is a combination of character digits and an underscore.
They are case-sensitive i.e., „num‟ and „Num‟ and „NUM‟ are three different
identifiers in python. It is a good programming practice to give meaningful
names to identifiers to make the code understandable.
We can also use the Python string isidentifier() method to check whether a
string is a valid identifier or not.
Python Variables
Last Updated : 25 Jan, 2025
In Python, variables are used to store data that can be referenced and manipulated
during program execution. A variable is essentially a name that is assigned to a value.
Unlike many other programming languages, Python variables do not require explicit
declaration of type. The type of the variable is inferred based on the value assigned.
Variables act as placeholders for data. They allow us to store and reuse values in our
program.
x=5
3
name = "Samantha"
6
print(x)
8
print(name)
Output
5
Samantha
In this article, we’ll explore the concept of variables in Python, including their syntax,
characteristics and common operations.
Python Comments
In the previous tutorial, you learned to write your first Python program. Now,
let's learn about Python comments..
Comments are hints that we add to our code to make it easier to understand.
Python comments start with # . For example,
# print a number
print(25)
Run Code
Single-line Comment
We use the hash ( # ) symbol to write a single-line comment. For example,
# declare a variable
name = "John"
# print name
print(name) # John
Run Code
# declare a variable
# print name
# John
A single-line comment starts with # and extends up to the end of the line.
We can also use single-line comments alongside the code:
print(name) # John
Multiline Comments
Unlike languages such as C++ and Java, Python doesn't have a dedicated
method to write multi-line comments.
However, we can achieve the same effect by using the hash ( # ) symbol at the
beginning of each line.
Let's look at an example.
'''This is an example
of multiline comment'''
print("Hello, World!")
Run Code
Output
Hello World
1. a = 5
We did not specify the type of the variable a, which has the value five from an integer. The
Python interpreter will automatically interpret the variable as an integer.
We can verify the type of the program-used variable thanks to Python. The type() function in
Python returns the type of the passed variable.
Consider the following illustration when defining and verifying the values of various data
types.
1. a=10
2. b="Hi Python"
3. c = 10.5
4. print(type(a))
5. print(type(b))
6. print(type(c))
Output:
<type 'int'>
<type 'str'>
<type 'float'>
The storage method for each of the standard data types that Python provides is specified by
Python. The following is a list of the Python-defined data types.
1. Numbers
2. Sequence Type
3. Boolean
4. Set
5. Dictionary
The data types will be briefly discussed in this tutorial section. We will talk about every
single one of them exhaustively later in this instructional exercise.
Numbers
Numeric values are stored in numbers. The whole number, float, and complex qualities
have a place with a Python Numbers datatype. Python offers the type() function to
determine a variable's data type. The instance () capability is utilized to check whether an
item has a place with a specific class.
When a number is assigned to a variable, Python generates Number objects. For instance,
1. a=5
2. print("The type of a", type(a))
3.
4. b = 40.5
5. print("The type of b", type(b))
6.
7. c = 1+3j
8. print("The type of c", type(c))
9. print(" c is a complex number", isinstance(1+3j,complex))
Output:
o Int: Whole number worth can be any length, like numbers 10, 2, 29, - 20, - 150,
and so on. An integer can be any length you want in Python. Its worth has a
place with int.
o Float: Float stores drifting point numbers like 1.9, 9.902, 15.2, etc. It can be
accurate to within 15 decimal places.
o Complex: An intricate number contains an arranged pair, i.e., x + iy, where x and
y signify the genuine and non-existent parts separately. The complex numbers
like 2.14j, 2.0 + 2.3j, etc.
Sequence Type
String
The sequence of characters in the quotation marks can be used to describe the string. A
string can be defined in Python using single, double, or triple quotes.
String dealing with Python is a direct undertaking since Python gives worked-in capabilities
and administrators to perform tasks in the string.
When dealing with strings, the operation "hello"+" python" returns "hello python," and the
operator + is used to combine two strings.
Example - 2
he
o
hello javatpointhello javatpoint
hello javatpoint how are you
List
Lists in Python are like arrays in C, but lists can contain data of different types. The things
put away in the rundown are isolated with a comma (,) and encased inside square sections
[].
To gain access to the list's data, we can use slice [:] operators. Like how they worked with
strings, the list is handled by the concatenation operator (+) and the repetition operator (*).
Example:
Tuple
In many ways, a tuple is like a list. Tuples, like lists, also contain a collection of items from
various data types. A parenthetical space () separates the tuple's components from one
another.
Because we cannot alter the size or value of the items in a tuple, it is a read-only data
structure.
Example:
<class 'tuple'>
('hi', 'Python', 2)
('Python', 2)
('hi',)
('hi', 'Python', 2, 'hi', 'Python', 2)
('hi', 'Python', 2, 'hi', 'Python', 2, 'hi', 'Python', 2)
Dictionary
A dictionary is a key-value pair set arranged in any order. It stores a specific value for each
key, like an associative array or a hash table. Value is any Python object, while the key can
hold any primitive data type.
The comma (,) and the curly braces are used to separate the items in the dictionary.
<class 'bool'>
<class 'bool'>
NameError: name 'false' is not defined
Set
The data type's unordered collection is Python Set. It is iterable, mutable(can change after
creation), and has remarkable components. The elements of a set have no set order; It
might return the element's altered sequence. Either a sequence of elements is passed
through the curly braces and separated by a comma to create the set or the built-in function
set() is used to create the set. It can contain different kinds of values.
Ans Print(“welcome”)