Introduction To Python Programming
Introduction To Python Programming
Features in Python
1.Free and Open Source
2. Easy to code
3. Easy to Read
4. Object-Oriented Language
5. GUI Programming Support
6. High-Level Language
7. Large Community Support
8. Easy to Debug
9. Python is a Portable language
10. Python is an Integrated language
3. Easy to Read
4. Object-Oriented Language
6. High-Level Language
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
With a new project py script, you can run and write Python codes in
HTML with the help of some simple tags <py-script>, <py-env>,
etc. This will help you do frontend development work in Python like
javascript. Backend is the strong forte of Python it’s extensively
used for this work cause of its frameworks like Django and Flask.
15. Allocating Memory Dynamically
History of Python
Python was developed by Guido van Rossum in the late eighties
and early nineties at the National Research Institute for
Mathematics and Computer Science in the Netherlands.
Python is derived from many other languages, including ABC,
Modula-3, C, C++, Algol-68, SmallTalk, and Unix shell and other
scripting languages.
Python is copyrighted. Like Perl, Python source code is now
available under the GNU General Public License (GPL).
Python is now maintained by a core development team at the
institute, although Guido van Rossum still holds a vital role in
directing its progress.
Python Variables
Creating Variables
2.A variable is created the moment you first assign a value to it.
x = 5
y = "John"
print(x)
print(y)
3.Variables do not need to be declared with any particular type, and can
even change type after they have been set.
x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)
4.Casting
If you want to specify the data type of a variable, this can be done with
casting.
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
You can get the data type of a variable with the type() function.
x = 5
y = "John"
print(type(x))
print(type(y))
6.Case-Sensitive
a = 4
A = "Sally"
#A will not overwrite a
Variables can store data of different types, and different types can do
different things.
Python has the following data types built-in by default, in these categories:
x = 20 int
x = 20.5 float
x = 1j complex
x = ["apple", "banana", "cherry"] list
x = range(6) range
x = True bool
x = b"Hello" bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview
x = None NoneType
x = int(20) int
x = float(20.5) float
x = complex(1j) complex
x = range(6) range
x = bytes(5) bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview
Python Operators
Python divides the operators in the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
Python Assignment Operators
Assignment operators are used to assign values to variables:
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
== Equal x == y
!= Not equal x != y
and Returns True if both statements are true x < 5 and x < 10
not Reverse the result, returns False if the not(x < 5 and x < 1
result is true
<< Zero fill Shift left by pushing zeros in from the right and let x << 2
left shift the leftmost bits fall off
>> Signed Shift right by pushing copies of the leftmost bit in x >> 2
right shift from the left, and let the rightmost bits fall off
i = 10
if (i == 10):
# First if statement
if (i < 15):
print("i is smaller than 15")
# Nested - if statement
# Will only be executed if statement above
# it is true
if (i < 12):
print("i is smaller than 12 too")
else:
print("i is greater than 15")
Python For Loops
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.
With the for loop we can execute a set of statements, once for each item
in a list, tuple, set etc.
Example
Print each fruit in a fruit list:
print(x)
for x in "banana":
print(x)