0% found this document useful (0 votes)
8 views

Codes learning w3school

The document provides an overview of variable declaration and manipulation in Python, including indentation, data types, and naming conventions. It explains how to assign values to variables, the importance of case sensitivity, and various ways to format multi-word variable names. Additionally, it covers built-in data types in Python and demonstrates examples of variable usage.

Uploaded by

Zana Kurdi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Codes learning w3school

The document provides an overview of variable declaration and manipulation in Python, including indentation, data types, and naming conventions. It explains how to assign values to variables, the importance of case sensitivity, and various ways to format multi-word variable names. Additionally, it covers built-in data types in Python and demonstrates examples of variable usage.

Uploaded by

Zana Kurdi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

 Indentation

if 5 > 2:
print("Five is greater than two!")

Example

 Check the Python version of the editor:

import sys

print(sys.version)

exit()

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

Example

x=4 # x is of type int


x = "Sally" # x is now of type str
print(x)

Casting
If you want to specify the data type of a variable, this can be done with casting.

Example

x = str(3) # x will be '3'


y = int(3) # y will be 3
z = float(3) # z will be 3.0

Single or Double Quotes?

String variables can be declared either by using single or double quotes:

Example

x = "John"
# is the same as
x = 'John'

Case-Sensitive

Variable names are case-sensitive.

Example

This will create two variables:

a=4
A = "Sally"
#A will not overwrite a

Try it Yourself »

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)

 A variable name cannot be any of the Python keywords.

Example

Illegal variable names:

2myvar = "John"
my-var = "John"
my var = "John"

Multi Words Variable Names

Variable names with more than one word can be difficult to read.

There are several techniques you can use to make them more readable:

Camel Case

Each word, except the first, starts with a capital letter:

myVariableName = "John"

Pascal Case

Each word starts with a capital letter:

MyVariableName = "John"
Snake Case

Each word is separated by an underscore character:

my_variable_name = "John"

Many Values to Multiple Variables

Python allows you to assign values to multiple variables in one line:

ExampleGet your own Python Server

x, y, z = "Orange", "Banana", "Cherry"


print(x)
print(y)
print(z)

Try it Yourself »

One Value to Multiple Variables

And you can assign the same value to multiple variables in one line:

Example

x = y = z = "Orange"
print(x)
print(y)
print(z)

Unpack a Collection

If you have a collection of values in a list, tuple etc. Python allows you to extract the values into
variables. This is called unpacking.

Example
Unpack a list:

fruits = ["apple", "banana", "cherry"]


x, y, z = fruits
print(x)
print(y)
print(z)

In the print() function, you output multiple variables, separated by a comma:

Example

x = "Python"
y = "is"
z = "awesome"
print(x, y, z)

You can also use the + operator to output multiple variables:


Example

x = "Python "
y = "is "
z = "awesome"
print(x + y + z)

Try it Yourself »

ExampleGet your own Python Server

Create a variable outside of a function, and use it inside the function

x = "awesome"

def myfunc():
print("Python is " + x)

myfunc()

Example

If you use the global keyword, the variable belongs to the global scope:

def myfunc():
global x
x = "fantastic"

myfunc()

print("Python is " + x)

Try it Yourself »

Also, use the global keyword if you want to change a global variable inside a function.
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

You might also like