print(“Hello, World!”)
#This is a comment
print("Hello, World!")
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.
x = 5
y = "John"
print(x)
print(y)
x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
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)
- A variable name cannot be any of the Python keywords.
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
x = y = z = "Orange"
print(x)
print(y)
print(z)
fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)
print(y)
print(z)
x = "Python is awesome"
print(x)
x = "Python"
y = "is"
z = "awesome"
print(x, y, z)
x = "Python "
y = "is "
z = "awesome"
print(x + y + z)
x = 5
y = 10
print(x + y)
Function
x = "awesome"
def myfunc():
print("Python is " + x)
myfunc()
x = "awesome"
def myfunc():
x = "fantastic"
print("Python is " + x)
myfunc()
print("Python is " + x)
Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function.
To create a global variable inside a function, you can use the global
keyword.
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
x = "awesome"
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
Built-in Data Types
In programming, data type is an important concept.
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 |
None Type: | NoneType |
x = 5
print(type(x))
Example | Data Type | Try it |
---|---|---|
x = “Hello World” | str | Try it » |
x = 20 | int | Try it » |
x = 20.5 | float | Try it » |
x = 1j | complex | Try it » |
x = [“apple”, “banana”, “cherry”] | list | Try it » |
x = (“apple”, “banana”, “cherry”) | tuple | Try it » |
x = range(6) | range | Try it » |
x = {“name” : “John”, “age” : 36} | dict | Try it » |
x = {“apple”, “banana”, “cherry”} | set | Try it » |
x = frozenset({“apple”, “banana”, “cherry”}) | frozenset | Try it » |
x = True | bool | Try it » |
x = b”Hello” | bytes | Try it » |
x = bytearray(5) | bytearray | Try it » |
x = memoryview(bytes(5)) | memoryview | Try it » |
x = None | NoneType | Try it » |
Setting the Specific Data Type
If you want to specify the data type, you can use the following constructor functions:
Example | Data Type | Try it |
---|---|---|
x = str(“Hello World”) | str | Try it » |
x = int(20) | int | Try it » |
x = float(20.5) | float | Try it » |
x = complex(1j) | complex | Try it » |
x = list((“apple”, “banana”, “cherry”)) | list | Try it » |
x = tuple((“apple”, “banana”, “cherry”)) | tuple | Try it » |
x = range(6) | range | Try it » |
x = dict(name=“John”, age=36) | dict | Try it » |
x = set((“apple”, “banana”, “cherry”)) | set | Try it » |
x = frozenset((“apple”, “banana”, “cherry”)) | frozenset | Try it » |
x = bool(5) | bool | Try it » |
x = bytes(5) | bytes | Try it » |
x = bytearray(5) | bytearray | Try it » |
x = memoryview(bytes(5)) | memoryview | Try it » |
Python Numbers
There are three numeric types in Python:
int
float
complex
x = 1 # int
y = 2.8 # float
z = 1j # complex
Integer
x = 1
y = 35656222554887711
z = -3255522
float
x = 1.10
y = 1.0
z = -35.59
Float can also be scientific numbers with an “e” to indicate the power of 10.
x = 35e3
y = 12E4
z = -87.7e100
Complex
Complex numbers are written with a “j” as the imaginary part:
x = 3+5j
y = 5j
z = -5j
Type Conversion
You can convert from one type to another with the int()
, float()
, and complex()
methods:
x = 1 # int
y = 2.8 # float
z = 1j # complex
#convert from int to float:
a = float(x)
#convert from float to int:
b = int(y)
#convert from int to complex:
c = complex(x)
Random Number
Python does not have a random()
function to make a random number, but Python has a built-in module called random
that can be used to make random numbers:
import random
print(random.randrange(1, 10))
Python Casting
Specify a Variable Type
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 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
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
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
w = float("4.2") # w will be 4.2
x = str("s1") # x will be 's1'
y = str(2) # y will be '2'
z = str(3.0) # z will be '3.0'