Sei sulla pagina 1di 13

PYTHON

Beginner to Advance
PYTHON
Python is an interpreted, object-oriented, high-level programming

language with dynamic semantics. Its high-level built in data

structures combined with dynamic typing and dynamic binding

make it very attractive for Rapid Application Development, as well

as for use as a scripting or glue language to connect existing

components together
PYTHON File Handling
The key function for working with files in Python is
the open() function.
The open() function takes two parameters; filename, and mode
There are four different methods (modes) for opening a file:

"r" - Read - Default value. Opens a file for reading, error if the file
does not exist
"a" - Append - Opens a file for appending, creates the file if it does
not exist
"w" - Write - Opens a file for writing, creates the file if it does not
exist
"x" - Create - Creates the specified file, returns an error if the file
exists

Syntax
f = open("demofile.txt")
PYTHON MYSQL
Python can be used in database applications.
One of the most popular databases is MySQL.

Install SQL Drivers


Python needs a MySQL driver to access the MySQL database.
In this tutorial we will use the driver "MySQL Connector".
We recommend that you use PIP to install "MySQL Connector".
PIP is most likely already installed in your Python environment.
Navigate your command line to the location of PIP, and type the following:
Download and install MySQL connector
Test MYSQL connector
To test if the installation was successful, or if you already have
"MySQL Connector" installed, create a Python page with the
following content

demo_mysql_Test.py:
import mysql.connector
Create Connection
Start by creating a connection to the database.
Use the username and password from your MySQL database:

demo_mysql_connection.py:

import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
passwd="yourpassword"
)

print(mydb)
PYTHON Comments
Comments can be used to explain Python code.
Comments can be used to make the code more readable.
Comments can be used to prevent execution when testing code.

Creating Comments
Comments starts with a #, and Python will ignore them:

Example
#This is a comment
print("Hello, World!")
PYTHON Variables
Variables are containers for storing data values.
Unlike other programming languages, Python has no command for
declaring a variable.
A variable is created the moment you first assign a value to it

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

Variables do not need to be declared with any particular type and can
even change type after they have been set.
PYTHON variable Names

A variable can have a short name (like x and y) or a more descriptive

name (age, carname, total_volume). Rules for Python variables: A

variable name must start with a letter or the underscore character

A variable name cannot start with a number

A variable name can only contain alpha-numeric characters and

underscores (A-z, 0-9, and _ )

Variable names are case-sensitive (age, Age and AGE are three

different variables)
PYTHON Data Types
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
Text Type: str
Numeric Types: int, float, complex

Sequence Types: list, tuple, range

Mapping Type: dict

Set Types: set, frozenset


Boolean Type: bool

Binary Types: bytes, bytearray, memoryview


PYTHON Numbers
There are three numeric types in Python:

❑ int
❑ float
❑ complex

Example
x = 1 # int
y = 2.8 # float
z = 1j # complex
PYTHON Casting
There may be times when you want to specify a type on to a
variable. This can be done with casting. Python is an object-
orientated language, and as such it uses classes to define data
types, including its primitive types.

Casting in python is therefore done using constructor functions:

int() - constructs an integer number from an integer literal, a float


literal (by rounding down to the previous whole number), 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
Examples
Integers
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3

Floats
x = float(1) # x will be 1.0
y = float(2.8) # y will be 2.8
z = float("3") # z will be 3.0

String
x = str("s1") # x will be 's1'
y = str(2) # y will be '2'
z = str(3.0) # z will be '3.0'

Potrebbero piacerti anche