Python: The Ultimate Python One Day Quickstart Guide. : Practical Python Programming For Beginners and Experts with Hands-on Project. by Pratiksha Sharma
Author:Pratiksha Sharma [Sharma, Pratiksha]
Language: eng
Format: epub
Published: 2017-03-08T08:00:00+00:00
Chapter 4 – Variables and Programs in Files
Naming a Variable
A variable name in Python can only contain letters (a - z, A - B), numbers or under- scores (_). However, the first character cannot be a number. Hence, you can name your variables user Name, user_ name or userName2 but not 2userName.
In addition, there are some reserved words that you cannot use as a variable name because they already have preassigned meanings in Python. These reserved words include words like print, input, if, while etc. We’ll learn about each of them in
subsequent chapters.
Finally, variable names are case sensitive. username is not the same as user_ Name. There are two conventions when naming a variable in Python. We can either use the camel case notation or use underscores. Camel case is the practice of writing compound words with mixed casing (e.g. thisIsAVariableName). This is the convention that we’ll be using in the rest of the book. Alternatively, another common
practice is to use underscores (_) to separate the words. If you prefer, you can name your variables like this: this_ is_ a_ variable_ name.
The Assignment Sign
Note that the = sign in the statement userAge = 0 has a different meaning from the = sign we learned in Math. In programming, the = sign is known as an assignment sign. It means we are assigning the value on the right side of the = sign to the variable on the left. A good way to understand the statement userAge = 0 is to think of it as userAge <- 0.
The statements x = y and y = x have very different meanings in programming. Confused? An example will likely clear this up.
Type the following code into your IDLE editor and save it.
x = 5 y = 10 x = y print ("x = ", x) print ("y = ", y)
Now run the program. You should get this output:
x = 10 y = 10
Although x has an initial value of 5 (declared on the first line), the third line x = y assigns the value of y to x (x <- y), hence changing the value of x to 10 while the
value of y remains unchanged.
Next, modify the program by changing ONLY ONE statement: Change the third line from x = y to y = x. Mathematically, x = y and y = x mean the same thing. How-
ever, this is not so in programming.
Run the second program. You will now get
x = 5 y = 5
You can see that in this example, the x value remains as 5, but the value of y is
changed to 5. This is because the statement y = x assigns the value of x to y (y <- x).
y becomes 5
while x remains unchanged as 5.
Basic Operators
Besides assigning a variable an initial value, we can also perform the usual mathematical operations on variables. Basic operators in Python include +, -, *, /, //, %
and ** which represent addition, subtraction, multiplication, division, floor division, modulus and exponent respectively.
Example:
Suppose x
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
What's Done in Darkness by Kayla Perrin(26519)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19005)
The Fifty Shades Trilogy & Grey by E L James(18955)
Shot Through the Heart by Mercy Celeste(18877)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(16979)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(16870)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(16800)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16693)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16319)
The Subtle Art of Not Giving a F*ck by Mark Manson(14253)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14069)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13416)
Scorched Earth by Nick Kyme(12710)
Drei Generationen auf dem Jakobsweg by Stein Pia(10919)
Suna by Ziefle Pia(10844)
Scythe by Neal Shusterman(10267)
International Relations from the Global South; Worlds of Difference; First Edition by Arlene B. Tickner & Karen Smith(9473)
Successful Proposal Strategies for Small Businesses: Using Knowledge Management ot Win Govenment, Private Sector, and International Contracts 3rd Edition by Robert Frey(9314)
This is Going to Hurt by Adam Kay(9092)
