Python Course for beginners

1)str.py file code :-

a = "Hello"
print(a)


 2)list.py file code:-


x = list(("apple", "banana", "cherry"))

#display x:
print(x)

#display the data type of x:
print(type(x))


3)tuple.py file code :-

x = tuple(("apple", "banana", "cherry"))

#display x:
print(x)

#display the data type of x:
print(type(x))



4)dict.py file code :-
x = {"name" : "John", "age" : 36}

#display x:
print(x)

#display the data type of x:
print(type(x))


5)set.py file code :-

x = {"apple", "banana", "cherry"}

#display x:
print(x)

#display the data type of x:
print(type(x))

6)var.py code :-

x = 5
y = "John"
print(x)
print(y)

7)

Python Numbers

There are three numeric types in Python:

  • int
  • float
  • complex

number.py file code :-

x = 1
y = 2.8
z = 1j

print(type(x))
print(type(y))
print(type(z))
...

8)Casting in python is therefore done using constructor functions:
  • int() - constructs an integer number from an integer literal, a float literal (by removing all decimals), or a string literal (providing the string represents a whole number)
  • float() - constructs a float number from an integer literal, a float literal or a string literal (providing the string represents a float or an integer)
  • str() - constructs a string from a wide variety of data types, including strings, integer literals and float literals
int.py file code :-
x = int(1)
y = int(2.8)
z = int("3")
print(x)
print(y)
print(z)


float.py file code :-
x = float(1)
y = float(2.8)
z = float("3")
w = float("4.2")
print(x)
print(y)
print(z)
print(w)


string.py file code :-
x = str("s1")
y = str(2)
z = str(3.0)
print(x)
print(y)
print(z)


9)if.py file code :-

a = 33
b = 200

if b > a:
  print("b is greater than a")


10)elif.py file code :-

a = 33
b = 33
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")


11)else.py file code :-
a = 200
b = 33
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
else:
  print("a is greater than b")

12)if-else.py code :-

a = 200
b = 33
if b > a:
  print("b is greater than a")
else:
  print("b is not greater than a")

13)while.py code :-
i = 1
while i < 6:
  print(i)
  i += 1


14)while-break.py code :-
i = 1
while i < 6:
  print(i)
  if (i == 3):
    break
  i += 1

15)while-continue.py code :-

i = 0
while i < 6:
  i += 1
  if i == 3:
    continue
  print(i)

16) for-loop.py code :-
fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)


17)for-loop1.py code :-
for x in "banana":
  print(x)


18) for loop exit when equal to banana

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)
  if x == "banana":
    break


19)for-loop-continue.py code :-

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  if x == "banana":
    continue
  print(x)

20)range.py file code:-

for x in range(2, 6):
  print(x)


21)incrementby3.py file code :-

Increment the sequence with 3 (default is 1):



for x in range(2, 30, 3):
  print(x)


22)function.py code :-

def my_function():
  print("Hello from a function")

my_function()

23)lambdafunction.py :-
x = lambda a: a + 10
print(x(5))


output: 15

24)lambdafunction1.py code :-

x = lambda a, b: a * b
print(x(5, 6))

output:-
30

25)lambdafunction2.py code :-


def myfunc(n):
  return lambda a : a * n

mydoubler = myfunc(2)
mytripler = myfunc(3)

print(mydoubler(11))
print(mytripler(11))
output:-
22
33

26)array.py file code:-

cars = ["Ford", "Volvo", "BMW"]

x = cars[0]

print(x)


27)class and object example  class.py file code :-


# Python3 program to show that we can create
# instance variables inside methods

# Class for Dog


class Dog:

    # Class Variable
    animal = 'dog'

    # The init method or constructor
    def __init__(self, breed):

        # Instance Variable
        self.breed = breed

    # Adds an instance variable
    def setRecord(self, color,name):
        self.color = color
        self.name = name

    # Retrieves instance variable
    def getRecord(self):
         print(self.color, self.name)
   
       
   


# Driver Code
Rodger = Dog("pug")
Rodger.setRecord("brown","puppy")
Rodger.getRecord()



28)mysql database with python 

step 1:- install xampp and run it and open any browser and type "localhost/phpmyadmin"

and then create database as "mydb"  and inside it create table customers with name and address column as shown below :-





mytest.py file code :-


import mysql.connector
mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="",
  database="mydb"
)

mycursor = mydb.cursor()

sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = ("omsir", "virarnagari 21")
mycursor.execute(sql, val)

mydb.commit()

print(mycursor.rowcount, "record inserted.") 


output:-


display table record :-


selectdatabase.py file code :-

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="",
  database="mydb"
)

mycursor = mydb.cursor()

mycursor.execute("SELECT * FROM customers")

myresult = mycursor.fetchall()

for x in myresult:
  print(x)

output:-

now to update record 

updatedatabase.py file code:-

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="",
  database="mydb"
)

mycursor = mydb.cursor()

sql = "UPDATE customers SET address = 'Canyon 123' WHERE address = 'virarnagari 21'"

mycursor.execute(sql)

mydb.commit()

print(mycursor.rowcount, "record(s) affected")

output:-


to delete record :-

deletedatabase.py file code :-

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="",
  database="mydb"
)

mycursor = mydb.cursor()

sql = "DELETE FROM customers WHERE name = 'omsir'"


mycursor.execute(sql)

mydb.commit()

print(mycursor.rowcount, "record(s) affected")

output:-



...
Previous
Next Post »