Hello Readers, We are going to dive into python language which is the most in-demand technology of the current generation.
Introduction
Python is a widely used high-level programming language. It was created by Guido van Rossum and first released in 1991.Python is easy to use, powerful, and versatile, making it a great choice for beginners and experts alike. Its unique features allows programmers to easily understand the concepts of python which makes them love python more than any other programming language.
Features of Python
Python has vast number of features. Let me list a few among them.
1. Easy to code.
3. Object-Oriented Language.
4. GUI Programming Support.
5. High-Level Language.
6. Extensible feature.
7. Python is Portable language.
8. Python is Integrated language.
9. Interpreted Language.
10. Large Standard Library.
11. Dynamically Typed Language.
Variables:
A Python variable is a reserved memory location to store values. In other words, a variable in a python program gives data to the computer for processing.
How to create a variable?
To create a variable in Python, all you need to do is specify the variable name, and then assign a value to it.
<variable name> = <value>
example:
#integer variable
a=10
#float variable
a=3.5
#string variable
a="Ramu"
Variable assignment works from left to right. So the following will give you an syntax error.
0 = x
=> Output:
SyntaxError: can't assign to literal
Rules for Creating a Variable:
1. Variables names must start with a letter or an underscore.
x = True # valid
_y = True # valid
9x = False # starts with numeral
=> SyntaxError: invalid syntax
$y = False # starts with symbol
=> SyntaxError: invalid syntax
2.The remainder of your variable name may consist of letters, numbers and underscores.
has_0_in_it = "Still Valid"
3. Names are case sensitive.
x = 9
y = X*5
=>NameError: name 'X' is not defined
Type Function:
The Python interpreter automatically picks the most suitable built-in type for the variable while declaring it. The type function is used for showing the type of the variable.
a = 2
print(type(a))
# Output: <type 'int'>
Packing and Unpacking :
You can assign multiple values to multiple variables in one line. Note that there must be the same number of arguments on the right and left sides of the = operator.
a, b, c = 1, 2, 3
print(a, b, c) # Output: 1 2 3
a, b, c = 1, 2
=> Traceback (most recent call last):
=> File "name.py", line N, in
Strings
Strings in Python are arrays of bytes representing Unicode characters. It can be represented using single quotes as well as double quotes. Strings are immutable. Python does not have a character data type, a single character is simply a string with a length of 1.
String Slicing:
You can return a range of characters by using the slice syntax. Specify the start index and the end index, separated by a colon, to return a part of the string.
b = "Hello, World!"
print(b[2:5])
Output: llo
Slice From the Start
By leaving out the start index, the range will start at the first character:
Example
Get the characters from the start to position 5 (not included):
b = "Hello, World!"
print(b[:5])
Output: "Hello"
Negative Indexing
Use negative indexes to start the slice from the end of the string:
b = "Hello, World!"
print(b[-5:-2])
Output: orl
String Concatenation:
To concatenate, or combine, two strings you can use the + operator.
a = "Hello"
b = "World"
c = a + b
print(c)
Output: HelloWorld
With Space,
a = "Hello"
b = "World"
c = a +" " +b
print(c)
Output: Hello World
Modification of String:
Strings are immutable.
S="Ramu"
S[1]="o"
TypeError: 'str' object does not support item assignment
Comments
Post a Comment