This article mainly introduces the variables in Python and Their Naming and printing. It is the basic knowledge in getting started with Python. If you need them, you can refer to the variable in the program as a name, this makes it easier for us to remember.
cars = 100 space_in_a_car = 4.0 drivers = 30 passengers = 90 cars_not_driven = cars - drivers cars_driven = drivers carpool_capacity = cars_driven * space_in_a_car average_passengers_per_car = passengers / cars_driven
print "There are", cars, "cars available." print "There are only", drivers, "drivers available." print "There will be", cars_not_driven, "empty cars today." print "We can transport", carpool_capacity, "people today." print "We have", passengers, "to carpool today." print "We need to put about", average_passengers_per_car, "in each car."
Tip: underlines are generally used in variable names to indicate hypothetical spaces. Make variable names more readable.
Running result:
root@he-desktop:~/mystuff# python ex4.py
There are 100 cars available.There are only 30 drivers available.There will be 70 empty cars today.We can transport 120.0 people today.We have 90 to carpool today.We need to put about 3 in each car.root@he-desktop:~/mystuff#
More variables and Printing
Now we enter more variables and print them. We usually use "" to refer to strings.
Strings are quite convenient. In practice, we will learn how to create strings containing variables. There is a special way to insert a variable into a string, which is equivalent to telling Python: "Hey, this is a formatted string. Put the variable here ."
Enter the following program:
# -- Coding: UTF-8 -- my_name = 'zed. shaw 'my_age = 35 # No lie Oh my_height = 74 # inch my_weight = 180 # Pound my_eyes = 'Blue 'my_teeth = 'white' my_hair = 'brown'
print "let's talk about %s." % my_name print "He's %d inches tall." % my_height print "He's %d pounds heavy." % my_weight print "Actually that's not too heavy." print "He's got %s eyes and %s hair." % (my_eyes, my_hair) print "His teeth are usually %s depending on the coffee." % my_teeth
# The following line is complex. Try to write it. Print "If I add % d, % d, and % d I get % d." % (my_age, my_height, my_weight, my_age + my_height + my_weight)
Tip: If the encoding is incorrect, enter the first sentence.
Running result:
root@he-desktop:~/mystuff# python ex5.py
let's talk about Zed A. Shaw.He's 74 inches tall.He's 180 pounds heavy.Actually that's not too heavy.He's got Blue eyes and Brown hair.His teeth are usually White depending on the coffee.If I add 35, 74, and 180 I get 289.root@he-desktop:~/mystuff#