Movatterモバイル変換


[0]ホーム

URL:


Python Wiki
Don't have an account?
Register
Sign In
Skip to content
Python Wiki
59
pages

Variables are an essential thing in all programming languages. They help us reuse multiple values more than just once.
One special thing about Python is theimplicit type-inference. This means, that we do not have to assign a type to a variable by its declaration as in Java or C (although thatis still possible, it's not required in Python). Basically, the compiler does the typing by itself.

Types in Python[]

Primitive Data Types[]

These following are called primitive, or, in other words, simple data types. Variables of these types can only hold one value.

Complex Data Types[]

Variable Name Rules[]

  1. Variable names can only be alphanumeric (i.e. can only contain letters and whole numbers).
  2. Variable names can only start with alphabetical letters or an underscore.
  3. Variable names can not be any of the Python keywords (i.e. you can not assign a variable with the name "if").
  4. Variable names are case-sensitive, so be sure that you're typing in the case you want.

Usage of variables[]

To assign a variable, we use this general Syntax:
variable_name = value

Now see some examples:

# declaring two stringsfoo='foo'bar='bar'# declaring an integer# note: automatically inferred as integersi1=7i2=42# declaring two floating point numbersd1=42.7d2=7.42# declaring two complex numbersc1=7+42jc2=42+7j

Reuse of variables[]

The main thing about variables is that we can use them more than just one time by calling them. This is done as in the following example:

# defining a variablefoo='awesome'# ... and reuse it by# printing it's valueprint(foo)#in Python 3.X# print foo   #in Python 2.X
Community content is available underCC-BY-SA unless otherwise noted.

[8]ページ先頭

©2009-2025 Movatter.jp