Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 22, 2022 09:19 pm GMT

Python all basics you need is here.

Introduction

  • Python is a general purpose high level programming language.
  • Python was developed by Guido Van Rossam in 1989 while working at National Research Institute at Netherlands.
  • But officially Python was made available to public in 1991.

Hello World in Python

Syntax : print("Hello World")

print("Hello World")
Hello World

IDENTIFIERS

  • A Name in Python Program is called Identifier.
  • It can be Class Name OR Function Name OR Module Name OR Variable Name.
  • a = 10

Rules to define Identifiers in Python:

  • alphabet symbols(either lower case or upper case), digits(0 to 9), and underscore symbol(_) are allowed characters in python.
  • Identifier should not start with numbers
  • Identifiers are case sensitive.
  • We cannot use reserved words as identifiers.

Note:

  • If identifier starts with _ symbol then it indicates that it is private
  • If identifier starts with __(Two Under Score Symbols) indicating that strongly private identifier.
# Examples for some valid and invalid identifiers

DATA TYPES

  • Data Type represents the type of data present inside a variable.
  • In Python we are not required to specify the type explicitly. Based on value provided, the type will be assigned automatically. Hence Python is dynamically Typed Language.

Python contains the following inbuilt data types

1) Int2) Float3) Complex4) Bool5) Str6) List7) Tuple8) Set9) Frozenset10) Dict11) None
  • type() method is used to check the type of variable

1) int Data Type:

  • to represent whole numbers (integral values)
  • Python directly supports arbitrary precision integers, also called infinite precision integers or bignums, as a top-level construct.
  • On a 64-bit machine, it corresponds to $2^{64 - 1}$ = 9,223,372,036,854,775,807
# Examples of Integer data Typea = 10b = -14c = 43095728934729279823748345345345345453453456345634653434363print(type(a))print(type(b))print(type(c))
<class 'int'><class 'int'><class 'int'>

2) Float Data Type:

  • We can use float data type to represent floating point values (decimal values)
  • We can also represent floating point values by using exponential form Eg: f = 1.2e3 (instead of 'e' we can use 'E')
# Examples of float data typea = 1.9b = 904304.0c = 1.2e3print(a)print(b)print(c)print(type(a))print(type(b))print(type(c))
1.9904304.01200.0<class 'float'><class 'float'><class 'float'>

3) Complex Data Type:

  • A complex number is of the form : (a + bj) where j = $\sqrt{-1}$.
  • we can perform operations on complex type values.
# Examples of complex data typea = 5 + 7jb = 19 - 3jprint('a = ',a)print('b = ',b)print(type(a))print(type(b))print(f'Subtraction is : {a - b}')print(f'Addition is : {a + b}')# Complex data type has inbuilt attributes imag and real typeprint(a.real)print(a.imag)
a =  (5+7j)b =  (19-3j)<class 'complex'><class 'complex'>Subtraction is : (-14+10j)Addition is : (24+4j)5.07.0

4) bool Data Type:

  • We can use this data type to represent boolean values.
  • The only allowed values for this data type are: True and False
  • Internally Python represents True as 1 and False as 0
# Examples of bool data typea = Trueb = Falseprint(type(a))print(type(b))
<class 'bool'><class 'bool'>

5) str Data Type:

  • str represents String data type.
  • A String is a sequence of characters enclosed within single quotes or double quotes.
# Examplesa = "Lets code..."print(a)
Lets code...
  • By using single quotes or double quotes we cannot represent multi line string literals.
  • For this requirement we should go for triple single quotes(''') or triple double quotes(""")
b = "Lets    code"
  File "/tmp/ipykernel_19/3804588111.py", line 1    b = "Lets             ^SyntaxError: EOL while scanning string literal
b = '''Lets    code'''print(b)

Slicing of Strings:

1) slice means a piece2) [ ] operator is called slice operator, which can be used to retrieve parts of String.3) In Python Strings follows zero based index.4) The index can be either +ve or -ve.5) +ve index means forward direction from Left to Right6) -ve index means backward direction from Right to Left
a = "Let's code great and change the world"print("a[0] : ", a[0])print("a[15] : ", a[15])print("a[-1] : ", a[-1])print("a[:5]", a[:5])print("a[7:14] : ", a[7:14])b = "Lets Code"print(b*3)print(len(b))

TYPE CASTING

  • We can convert one type value to another type. This conversion is called Typecasting.
  • The following are various inbuilt functions for type casting. 1) int() 2) float() 3) complex() 4) bool() 5) str()
print(int(3434.554))
print(int(6 + 6j))
print(str(32423))
print(int("five"))
print(int("2345"))
print(float(244))
print(bool(0))print(bool(2324))print(bool(-34))print(bool(0.0))print(bool(7.8))print(bool("False"))print(bool("True"))print(bool("Lets code"))

6) List Data Type:

  • Lists are used to store multiple items in a single variable.
  • Lists are created using square brackets
  • List items are ordered, changeable, and allow duplicate values.
  • List items are indexed, the first item has index [0], the second item has index [1] etc.
  • When we say that lists are ordered, it means that the items have a defined order, and that order will not change.
  • If you add new items to a list, the new items will be placed at the end of the list.
listt = [5, 6, 'hello', 5.76]print(listt)print(listt[0])print(listt[2])listt.append(376)print(listt)# Iterating over listfor i in listt:    print(i)

7) Tuple Data Type:

  • Tuples are used to store multiple items in a single variable.
  • A tuple is a collection which is ordered and unchangeable.
  • Tuples are written with round brackets.
  • Tuple items are ordered, unchangeable, and allow duplicate values.
  • Tuple items are indexed, the first item has index [0], the second item has index [1] etc.
  • Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been created.
tuple1 = ("abc", 34, True, 40, "male")print(tuple1)print(tuple[0])print(tuple[3])

8) set Data Type:

  • Sets are used to store multiple items in a single variable.
  • A set is a collection which is unordered, unchangeable*, and unindexed.
  • Sets are written with curly brackets.
  • Sets are unordered, so you cannot be sure in which order the items will appear.
  • Set items are unchangeable, but you can remove items and add new items.
  • Set items can appear in a different order every time you use them, and cannot be referred to by index or key.
  • Sets cannot have two items with the same value.
set1 = {"abc", 34, True, 40, "male", 40}print(set1)set1.add(67)print(set1)set1.remove("abc")print(set1)

9) frozenset Data Type:

  • It is exactly same as set except that it is immutable.
  • Hence we cannot use add or remove functions.
s={10,20,30,40}frozen_set=frozenset(s)print(type(frozen_set))print(frozen_set)

10) dict Data Type:

  • Dictionaries are used to store data values in key:value pairs.
  • A dictionary is a collection which is ordered*, changeable and do not allow duplicates.
  • Dictionaries are written with curly brackets, and have keys and values.
  • Dictionary items are ordered, changeable, and does not allow duplicates.
  • Dictionary items are presented in key:value pairs, and can be referred to by using the key name.
  • Dictionaries cannot have two items with the same key.
dict1 = {  "brand": "Ford",  "electric": False,  "year": 1964,  "colors": ["red", "white", "blue"]}print(dict1)print(dict1["colors"])print(dict1["colors"][1])

11) None Data Type:

  • None means nothing or No value associated.
  • If the value is not available, then to handle such type of cases None introduced.
def sum(a,b):    c = a + bs = sum(5,7)print(s)

Hope you like this


Original Link: https://dev.to/anurag629/python-all-basics-you-need-is-here-2jj8

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To